Printing the name of a variable

Ben Finney ben+python at benfinney.id.au
Thu Sep 9 22:05:49 EDT 2010


Albert Hopkins <marduk at letterboxes.org> writes:

> On Thu, 2010-09-09 at 12:43 -0700, Stephen Boulet wrote:
> > Does an arbitrary variable carry an attribute describing the text in
> > its name?
>
> Variables are not objects and so they have no attributes.

Another way of thinking about is that there are no variables, only
bindings between references and objects. The bindings are not directly
accessible and have no attributes.

Any object may have zero, one, or many references to it at any given
time. Those references might be names, or something else (such as an
index in a container).

Python does nothing to make the references accessible to the object. If
you want to keep track of such associations, you need to employ some
mechanism to do so explicitly.

> You can't really de-reference the object being referenced as it can
> potentially contain multiple references

Including the case where an object has no explicit references beyond the
current expression.

> But this cries the question: why do you want to do this? And usually
> that question is asked when someone thinks that a: you shouldn't need
> to do this and b: whatever the desired effect there is probably a
> better way of accomplishing it.

To anticipate a common case: if you feel you need to maintain a mapping
between names and objects, Python's built-in mapping type is ‘dict’; use
that.

-- 
 \           “If you do not trust the source do not use this program.” |
  `\                                —Microsoft Vista security dialogue |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list