Parsing & question about usages off classes!

Peter Hansen peter at engcorp.com
Sun Nov 3 15:30:01 EST 2002


Frans wrote:
> Hi,
> 
> I want to put each line off a logfile in a different class. But I cant 
> seem to, dynamicly, create new classes. I am having problems using 
> variables when creating new classes.
> 
> class TEST:
>     pass
> 
> x=0
> while x < 10:
>     A + "" + x = TEST()
>     x=x+1
> 
> Something like that is what I want :)
> 
> Having A1 A2 A3 A4 etc. etc. as TEST classes.

Building on Alex' good advice, here's what you
really wanted to do (I'm guessing) in the first
place.  You may need to learn more about Python
to make use of these results:

myObjects = {}
for x in range(10):
    myObjects['A' + str(x)] = TEST()

print myObjects
print myObjects['A6']

OR this one, where they don't each get their own "name":

myObjects = []
for x in range(10):
    myObjects.append(TEST())

print myObjects
print myObjects[3]


If this looks remotely like what you wanted, then you should know
you are not creating new *classes*, but only new instances of
the one class, or new "objects".

You might also want to go back and rethink the whole approach.
You say you want each line to go into a different "object" (if
I may rephrase your request), but what do you plan to do with
those objects?  Maybe you can take a simpler approach to getting
the task done.  What's your goal?

-Peter




More information about the Python-list mailing list