[Tutor] Dynamically instantiating objects?

Bryce Embry bryce@bembry.org
Fri, 12 Apr 2002 11:01:28 -0500


Israel,
I tried the same thing a few weeks ago.  The trick is to use a dictionary, 
like the following:

 >>> list = ("dog", "cat", "fish")
 >>> dict={}
 >>> class play:
         def __init__(self, name):
                 self.var = name
         def display(self):
                 print self.var


 >>> for item in list:
         dict[item]=play(item)


 >>> dict["dog"].display()
dog
 >>> dict["cat"].display()
cat
 >>> dict["fish"].display()
fish
 >>>

That's a quick answer.  There is a lot more detail in the OOPs thread from 
last month. at http://mail.python.org/pipermail/tutor/2002-March/thread.html.

Bryce Embry



--------------------------------------------------------------------------------------------
"Lord, you establish peace for us.
All that we have accomplished
you have done for us" -- Isaiah 26:12