Printable string for 'self'
mtobis at gmail.com
Thu Mar 16 08:00:27 CET 2006
This behavior seems to be commonly wanted by people discovering Python,
and it is the rare case of something one can imagine that is really a
stretch to achieve in Python. Because more or less than one name may
refer to an object, in general an object can't know its name.
You can get part of the way there with
fred = C("fred")
and assign the string to self.name in the constructor. But you must
strictly enforce a convention in your code that the reference fred is
not reassigned to another referent for this to be of much use.
I got in some trouble in these parts a few months back for advocating
some sort of immutable reference, like
fred -> C("fred")
where any reassignment of the refernce during the lifetime of the
referent would raise an exception. This seems to be seen as wrongheaded
by greater pythonistas than myself. I don't fully understand *why*
this is a bad idea, but my intuitive idea that it would be very
valuable has gone away.
More information about the Python-list