Re: [Python] Metodo __str__(self):
Il 20 ottobre 2017 08:52, Gabriele Battaglia ha scritto: > Giorno. > Al metodo __str__ di un oggetto, è possibile passare dei parametri? > Se ad esempio voglio che l'oggetto stampato si comporti in maniera diversa a > seconda di come invoco il metodo print su di esso, posso farlo oppure > __str__ vuole solo (self), come parametro? > > Se posso farlo, come si fa? > > Ammettiamo che abbia un oggetto che contiene 2 variabili. Posso chiamare il > suo metodo __str__ con una print in 2 modi diversi, affinchè l'oggetto mi > restituisca l'una o l'altra variabile? Invece che un metodo __str__ con un parametro, forse non sarebbe meglio usare metodi diversi per risultati diversi? Comunque io ho ottenuto un effetto simile a quello che vuoi tu, aggiungendo un attributo all'oggetto e controllando il valore di quell'attributo all'interno del metodo __str__. Se può servire... Ciao -- Daniele www.fugamatematica.blogspot.com giusto! nel verso forse è perché non guardiamo le cose Quando non ci capiamo, ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Metodo __str__(self):
2017-10-20 8:52 GMT+02:00 Gabriele Battaglia : > Al metodo __str__ di un oggetto, è possibile passare dei parametri? > No, ma potresti usare __format__() https://docs.python.org/3/library/stdtypes.html#str.format https://docs.python.org/3/library/string.html#formatspec https://pyformat.info/#custom_1 https://gist.github.com/drmalex07/d80643716263bb18f346 -- | Raffaele Salmaso | https://salmaso.org | https://bitbucket.org/rsalmaso | https://github.com/rsalmaso ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Metodo __str__(self):
-BEGIN PGP SIGNED MESSAGE- Hash: SHA256 Il 20/10/17 08:52, Gabriele Battaglia ha scritto: > > Ammettiamo che abbia un oggetto che contiene 2 variabili. Posso > chiamare il suo metodo __str__ con una print in 2 modi diversi, > affinchè l'oggetto mi restituisca l'una o l'altra variabile? lo sovrascrivi con un metodo che faccia le cose che ti servono. -BEGIN PGP SIGNATURE- Comment: GPGTools - http://gpgtools.org iQFGBAEBCAAwFiEEklR7hkX4M02p083YYEsbdHj78WUFAlnpolcSHG1hdXJvQHRl cHBpc3RpLml0AAoJEGBLG3R4+/Fl87gH/1qSKvY2JRYWBHW8kLwGeg4+/XI+MdDX bLeQt4GRirR5tsr1UIiLRLbCdhSmMp+AiBui7bUVuDmuaBf5AfHYUUKwfx2ZeAFd bGOjqPYw7J0hJhyTY3PX0Cj/H24+nbGylLmqFs7N1XPWzDWCiTVtZIhYRbGlmGWD dwu7Q6Ola5MTrqbCDvbN1qdgBPQaV3DmLp1Jc82NfM/l0zQR6O2AnKq6TI93xK7W E5QxlcBFezoQCWZdLCLGQy7d+HR7IG/Isd8s0N9OAxBdrZ3+n+w8Uih9c8JsBibY uKMKcf0s9ScWCqwe8Iy31ic687PXz0WCpIlZlru2mVrNQCwWJzobCwI= =v8eD -END PGP SIGNATURE- ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python