Re: Printing the name of a variable

2010-09-10 Thread Paul Rudin
Stephen Boulet stephen.bou...@gmail.com writes:

 Does an arbitrary variable carry an attribute describing the text in
 its name? I'm looking for something along the lines of:

 x = 10
 print x.name
 'x'

 Perhaps the x.__getattribute__ method? Thanks.

The first thing is... what is your use case for this? I'd guess that you
probably don't want to do this even if you think you do :)

The thing referred to by x is the number 10. When you write x.name (or
property) then you're dealing with the number 10, not with some
representation of the variable x. There may be many variables (or none)
that refer to that number at any given time during the execution of your
code... and the object itself knows nothing about any of these.

A good way to think about variable lookup is as a dictionary . It's
something like variables['x'].name. Once the varables['x'] bit has
been evaluated the link with 'x' is gone... we just have the result of
the lookup.

If you want to know how it actually works, then read up on python
namespaces and scopes, e.g. here:
http://www.network-theory.co.uk/docs/pytut/PythonScopesandNameSpaces.html.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-10 Thread Jonathan Hartley
On Sep 9, 9:11 pm, Albert Hopkins mar...@letterboxes.org wrote:
 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? I'm looking for something along the lines of:

  x = 10
  print x.name
   'x'

  Perhaps the x.__getattribute__ method? Thanks.

 Variables are not objects and so they have no attributes.

 You can't really de-reference the object being referenced as it can
 potentially contain multiple references, but an innacurate way of doing
 this would be, e.g.

  x = [1, 2, 3]
  y = x
  g = globals()
  varnames = [i for i in g if g[i] is x]

 ['x', 'y']

 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.

I have in the past wondered about creating a kind of graphical
debugger, that rendered representations of all the objects in globals
and/or locals, to give you a visual representation of your variables
and their states. Would this be a valid use case to try and look up
the variable names which reference various in-memory objects?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-10 Thread Simon Brunning
On 9 September 2010 20:43, Stephen Boulet stephen.bou...@gmail.com wrote:
 Does an arbitrary variable carry an attribute describing the text in
 its name? I'm looking for something along the lines of:

 x = 10
 print x.name
 'x'

http://effbot.org/pyfaq/how-can-my-code-discover-the-name-of-an-object.htm

-- 
Cheers,
Simon B.
-- 
http://mail.python.org/mailman/listinfo/python-list


Printing the name of a variable

2010-09-09 Thread Stephen Boulet
Does an arbitrary variable carry an attribute describing the text in
its name? I'm looking for something along the lines of:

x = 10
print x.name
 'x'

Perhaps the x.__getattribute__ method? Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-09 Thread Albert Hopkins
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? I'm looking for something along the lines of:
 
 x = 10
 print x.name
  'x'
 
 Perhaps the x.__getattribute__ method? Thanks.

Variables are not objects and so they have no attributes.

You can't really de-reference the object being referenced as it can
potentially contain multiple references, but an innacurate way of doing
this would be, e.g.

 x = [1, 2, 3]
 y = x
 g = globals() 
 varnames = [i for i in g if g[i] is x]
['x', 'y']

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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-09 Thread Terry Reedy

On 9/9/2010 3:43 PM, Stephen Boulet wrote:

In Python, 'variable' is a synonym for name and identifier,'

An object can have 0 to many names attached to it. An object can also be 
a member of 0 to many collections.


Modules, classes, and functions have 'definition name' strings attached 
to them as their .__name__ attribute. This attribute is used to display 
the 'value' of these objects, as in

 def f(): pass

 f
function f at 0x00F05420

The __name__ attribute is also used in the idiom
 if __name__ == '__main__': print(I am the main module)

I am the main module

to determine if a module is being run as the main module as imported 
into another module.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-09 Thread Ben Finney
Albert Hopkins mar...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing the name of a variable

2010-09-09 Thread Mel
Stephen Boulet wrote:

 Does an arbitrary variable carry an attribute describing the text in
 its name? I'm looking for something along the lines of:
 
 x = 10
 print x.name
 'x'
 
 Perhaps the x.__getattribute__ method? Thanks.

Hmm.  Recent scholarship suggests that the Iliad and the Odyssey may not 
have been written by Homer, but by someone else with the same name.

Mel.

-- 
http://mail.python.org/mailman/listinfo/python-list