How to get an object's name as a string?

Joe Strout joe at strout.net
Tue Oct 28 21:02:48 EDT 2008


On Oct 28, 2008, at 4:45 PM, Steven D'Aprano wrote:

>> What do you mean by the "name" of an object?  Objects don't generally
>> have names, unless you explicitly define a .name property and assign
>> them names.
>>
>> (Variables have names, of course, but a variable isn't an object --  
>> it's
>> just a reference to an object.  Many variables may refer to the same
>> object, so it doesn't make any sense to ask for the name of THE  
>> variable
>> which may be referring to an object at the moment.)
>
> That explanation makes no sense. Given the assignment:
>
> x = 57
>
> if the name of x isn't 'x', then what on earth can it possibly mean to
> ask for the name of a variable?

Perhaps you're skimming rather than reading carefully?  Variables do  
have names, as I pointed out, and the name of x is indeed 'x'.  But  
that's not what the OP was asking for -- in your example, he'd be  
asking for the name of 57 (expecting it to be 'x').  Numbers don't  
have names; objects don't have names; variables have names, and may  
refer to numbers or objects.

> In languages like Python, the term "variable" is misleading and
> confusing.

Oh good grief.  Now you're going to try to discard the standard term  
"variable" as well?

All right then, if you really insist on making Python more mysterious  
by making up new terms for perfectly ordinary and standard programming  
concepts, then I suggest the following:

variable: "ariablevay"
value: "aluvay"
reference: "eferencevay"
call-by-value: "allcay-ibay-aluvay"
call-by-reference: (no term needed, since Python doesn't have it)

There.  Now we've got a simple mapping from standard terminology to  
properly mystical Python-culture terms that are nonetheless easy to  
learn.  Agreed?

Best,
- Joe

P.S. Shannon: don't listen to Steven.  He's out to confuse you and  
make Python seem much harder and complex than it really is.




More information about the Python-list mailing list