[Tutor] How to generate instance names?
Christopher Singley
csingley at gmail.com
Mon Nov 8 21:29:11 CET 2004
I am new to object-oriented programming, and have a basic question.
How can I generate an object name when creating an instance of a
class, rather than hard-coding the name at creation?
I'd like to say something like this:
--
class Person(object):
def __init__(self,name):
self.name = name
names = ["Tom", "Dick", "Harry"]
personlist = [ ]
i=0
for name in names:
dude"i" = Person(name)
personlist.append(dude"i")
i += 1
--
in order to create dude1 with dude1.name = "Tom",
dude2 with dude2.name = "Dick", and so forth.
How can this be done?
A second question: how can i define a function that takes as input an
integer i, and returns the name of the object at personlist[ i ] (in
this case, dude"i")?
Also a meta-question: how can I search for the answers to these types
of Python-programming questions?
TIA
cs
More information about the Tutor
mailing list