How to get an object's name as a string?
Larry Bates
larry.bates at vitalEsafe.com
Tue Oct 28 10:57:53 EDT 2008
Shannon Mayne wrote:
> I would like to create objects with algorithmically determined names
> based on other object names and use object names for general algorithm
> input.
>
> How would one extract the name of an object from an object instance as
> a string. I would think that it is stored as an attribute of the
> object but successive 'dir()' calles haven't found me the attribute
> with the namestring.
>
> My thanks!
>
Once again (there have been many posts on this subject). Objects can have more
than one name in Python. Therefore there is not a one-to-one correspondence
between an object instance and name(s) that point to it.
Example:
a = myObject()
b = a
c = a
now a, b, c all point to the same object. How would you define it's "name".
You are certainly free to store a name as an attribute to the object, but the
linkage would then be purely logical.
Example:
objects = []
objects.append(myObject('a'))
#
# Find object with name == 'a'
#
obj = None
for object in objects:
if object.name == 'a':
obj = object
-Larry
More information about the Python-list
mailing list