Python 3.0 - Dynamic Class Instance Naming

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2011-01-05T20:15:03Z Indexed on 2011/01/06 6:53 UTC
Read the original article Hit count: 224

Filed under:
|

I want to use a while loop to initialize class objects with a simple incremented naming convention. The goal is to be able to scale the number of class objects at will and have the program generate the names automatically. (ex. h1...h100...h1000...) Each h1,h2,h3... being its own instance.

Here is my first attempt... have been unable to find a good example.

class Korker(object):

     def  __init__(self,ident,roo):
             self.ident = ident
             self.roo = roo


b = 1
hwinit = 'h'
hwstart = 0

while b <= 10:
    showit = 'h' + str(b)
    print(showit)   #showit seems to generate just fine as demonstrated by print
    str(showit) == Korker("test",2)   #this is the line that fails

    b += 1

The errors I get range from a string error to a cannot use function type error.... Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x