[Tutor] Dynamic creation of class instances...

Israel Evans israel@lith.com
Wed, 22 May 2002 07:58:58 -0700


 I don't know _exactly_ why I figured that I need this sort of thing, but I
believe that it comes from a misunderstanding of how to OOP really works.
In my case I wanted to have a situation where a user would end up creating
an object of sorts that I would hopefully use later on in the program.  
I was thinking along the lines of:
"How can I use an unknown object later on if I don't know it's name?
Everything needs a name right?  How can I even make the darn thing without a
unique name?"

I thought it would be easier to create an instance with some name that the
user specified and then introduce that nice little object to whatever object
manager thingy-doodle I've crafted to handle such objects than to do it any
other way.  Actually I couldn't quite see how to handle new objects created
by a program.  Either by a user or perhaps by some sort of agent. It
befuddled me to see how I could make an object without giving it a name.  I
think a good month bathing in pulped up OOP and Computer Science books would
help me to absorb some of what I'm missing.

My understanding of programming is rather darn foggy and limited so that was
the first best thought as to a solution.  I'd love to hear of better ways or
be introduced to a couple of really nice books.



~Israel~


-----Original Message-----
From: alan.gauld@bt.com [mailto:alan.gauld@bt.com] 
Sent: 22 May 2002 6:02 AM
To: israel@lith.com; tutor@python.org
Subject: RE: [Tutor] Dynamic creation of class instances...

>  Right now I start out with a list of names, craft a string that 
>  looks like a class instantiation and then exec the sucker.  

Paul has already answered this based on a dictionary solution.

But this is an issue for everyone on the list.

Why does this same question come up every month on this list?
It has to be one of the top FAQs for this mailing list.

It suggests to me that something in the standard tutorials/texts 
must be suggesting this solution to people. Its such a bizarre
solution that it never seems to come up in any of the other 
fora that I engage with(Delphi, C++, Smalltalk, Java(spit!) ).

But on Python it seems like most newbies sooner or later come 
up with this amazing idea for naming objects using exec.

So Why? Comments welcomed from newbies who've been there, or 
from newbies whpo nearly went there or from exorerienced hackers 
who might know why it comes up so often?

I am seriously puzzled and if I can do anything in my tutor to
circumvent it(short of discussing it explicitly!) I will.

Alan g.