Extracting name strings from assigned objects

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Oct 28 18:37:17 EDT 2008


On Tue, 28 Oct 2008 09:23:46 -0700, Shannon Mayne wrote:

> I would like to ask how one might obtain the assigned name of an
> assigned object as a string. I would like to use object names as an
> algorithmic input.
> 
> 
> To demonstrate... So if i have:
> 
>>>foo = {}
> 
> what can I do to the object 'foo' so as to return the string 'foo'?

There's a lot of confusion there. The object 'foo' is a string, and it 
doesn't occur anywhere in your code. The name foo is not an object, it is 
a name. Perhaps you meant the object {} (an empty dict)?

The object {} doesn't know what names (note plural) it has been bound 
too. It could be bound to one name:

foo = {}

or many names:

foo = bar = baz = flib = {}

or no names at all:

{}


Names are not properties of objects. You can't do what you are trying to 
do. If you tell us why you want to do this, perhaps we can suggest a way 
to do it.



-- 
Steven



More information about the Python-list mailing list