How to Get the Object from String/Label?
William Djaja Tjokroaminata
billtj at y.glue.umd.edu
Tue Sep 12 13:20:26 EDT 2000
Hi,
I have a problem of converting a string/label to the corresponding
object. Say I have an object:
class MyObject:
def __init__ (self, myName):
self.myName = myName
myObject = MyObject ('xyz')
Now, if I have the string 'myObject', how can I get the real object?
myString = 'myObject'
exec 'theObject = myString'
This does not work, as theObject refers to the string myObject and not the
object myObject. In practice, myString gets the string/label not from a
simple assignement such as above but from some other string process.
Is there any way of easily converting the string myObject to the object
myObject without having to loop through all the objects and find a match
to the object with self.myName == 'xyz'?
Thanks.
Regards,
Bill
More information about the Python-list
mailing list