[Tutor] Accessing the name of an instance variable
op73418 at mail.telepac.pt
Sun Nov 16 07:41:35 EST 2003
On Sun, 16 Nov 2003 11:05:13 -0000, you wrote:
>For debugging purposes, I would like to be able to print the name of some
>instances of classes directly from the program, together with the class
>data. The data is of course simple but getting the name is a problem to me.
>The best I have come up with so far is vars(), from which I can scan for
>instances of the relevant classes.
>However the name of the instance is returned as a string. How would I get my
>hands on the actual instance from this in order to get at its instance so as
>to print the data? Or is there a better way of going about what I need to
Variables in Python are just names (e.g. strings) that are bounded (or
reference) some value, a Python object. Object's per se are nameless.
A collection of names, values is called a namespace, and is currently
implemented as a dictionary.
The following shows that, as in real life, an object can have more
than one name.
>>> a = b = 
>>> print a is b
Having said this, look at the globals, locals functions
>>> print globals.__doc__
globals() -> dictionary
Return the dictionary containing the current scope's global variables.
>>> print locals.__doc__
locals() -> dictionary
Update and return a dictionary containing the current scope's local
As a final note, shouldn't you be using a *debugger* for debugging
purposes? Also consider the unittest module for testing your code. If
well exercised you can dispense with a debugger altogether.
With my best regards,
More information about the Tutor