How to obtain an instance's name at runtime?

Darrell news at dorb.com
Wed Jun 23 16:38:40 EDT 1999


Maybe I'm missing the point here.
I use this approach which shows the class name and an address for the
instance. That's about as good a name as I can imagine possible for that
instance.  Unless you start adding name attributes.

>>> class A:
...     pass
...
>>> a=A()
>>> str(a)
'<__main__.A instance at 7f6350>'

If you want to keep track of the name of the variable that your instance is
contained within. Then I don't think this is possible because it would
require that each instance keep a list of the variables that it was stored
in. Even then you would be faced with a list instead of a single name.
>>> str(b)
'<__main__.A instance at 7f6350>'
--
--Darrell






More information about the Python-list mailing list