[Tutor] creating an object instance w/out hard-coding the
name
Magnus Lycka
magnus@thinkware.se
Fri, 18 Oct 2002 11:58:36 +0200
At 16:08 2002-10-17 -0400, dlphone@bellsouth.net wrote:
>Hello all:
>
>I would like to create a new object instance from user input, or using an
>iterated variable, for a small OODB. Can this be done? Assuming that
>'Project' is my class, then Creating an instance as follows won't work,
>because the new instance will literally be named 'projName'.
>
>projName = raw_input("Name your project")
>projName = Project()
You *can* do this, but you shouldn't. It would only cause
sorrows. The only one to introduce variable names in the
program should be the programmer. I think you would have
figured that out in time. It would have been extremely hard
to get the rest of the program to work. But go on coding, I
think the use of variable names will become clearer.
I suggest you place the projects in a dictionary.
projects = {}
...
projName = raw_input("Name your project: ")
if projects.has_key(projName):
print "That name is already taken."
...
else:
projects[projName] = Project()
...
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se