[Tutor] How to generate instance names?
Kent Johnson
kent_johnson at skillsoft.com
Mon Nov 8 23:54:54 CET 2004
At 04:22 PM 11/8/2004 -0600, Christopher Singley wrote:
>I am writing a Python program to manage a SQL database. I wish to
>represent database tables by object classes, and table rows by
>instances of classes. Object attributes correspond to table columns.
>A SQL query causes object instantiation; since there will be an
>unknown number of query results, I'm looking for a way to generate
>arbitrary numbers of instances. I had thought to package the
>query-objects into lists and iterate over them; each object in the
>list can be processed by invoking its methods.
>
>Is this a doomed plan? Is there a better way, or a standard solution
>for this problem?
This is a fine plan. The only flaw is thinking that you have to give a
different name to each instance :-)
Before I answer your question, let me point out that this is a common
desire. It is pretty easy to create a simple solution yourself, but other
people have already invented ways to do it. SQLObject seems to be one of
the more popular: http://sqlobject.org/
OK, about names...a name is not a property of an object or a container for
the object. It is more like a pointer to the object, or a label you stick
on the object so you can refer to it. This article may help you understand:
http://effbot.org/zone/python-objects.htm
You don't need a unique name for each object, you just need some way of
keeping track of it. Other ways of keeping track of objects are to put them
in a list or dictionary. You can make a list of objects without giving the
objects any name at all.
Referring to your original post:
>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
All you really care about here is personlist. The name is temporary and of
little consequence. You could write this loop more simply like this:
names = ["Tom", "Dick", "Harry"]
personlist = [ ]
for name in names:
dude = Person(name)
personlist.append(dude)
You don't need to assign Person to a name at all - this works just as well:
personlist = [ ]
for name in names:
personlist.append(Person(name))
or you can use a list comprehension and make it really short and sweet:
personlist = [ Person(name) for name in names ]
All three have the same result - personlist is a list containing three
Person objects with the names Tom, Dick and Harry.
>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")?
You want to return the value - the actual object - not the name. The name
is just a way of talking about the value. All you have to do is index
personlist:
def getPerson(i):
return personlist[i]
>Also a meta-question: how can I search for the answers to these types
>of Python-programming questions?
You can search the Tutor list at ActiveState:
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor
The comp.lang.python newsgroup is a more advanced resource. You can search
it from Google groups:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&group=comp.lang.python
Kent
More information about the Tutor
mailing list