list - How can I create an indefinite iterations of objects in python? -
i'm new python , trying create program test methods of object creation. currently, i'm writing program involves creating objects, giving them unique numeric variable, , assigning them list future referencing. here's wrote create variable names:
def getrectanglename(): rectname = list("rectangle") spawnedobjectlist.append(len(spawnedobjectlist)) rectname.append(str(len(spawnedobjectlist))) return rectname
and that's passed onto turn string variable name. tried eval(), learned bad reason , didn't work anyway, , tried workarounds no avail.
i figure there's plenty of games have indefinite number of characters on screen. there established way of making iterations of objects this?
the objects have x , y act reference points display of rectangles on screen(the idea in future have each 1 move around on own, making lists of x , y draw rectangles isn't useful).
edit: problem don't know how give each object own variable put on list future referencing.
edit2: don't think i'm asking right question, actually, or using right terminology. need able have indefinite number of objects created on fly after program running, , able reference them individually.
if dynamically want create variables , add them class instances, use this
class mainclass: def __setattr__(self, name, value): self.__dict__[name] = value def getrectanglenamegenerator(n = 10): x = 0 while x <= n: x += 1 yield "rectangle" + str(x) rectanglename = getrectanglenamegenerator() classinstances = {next(rectanglename) : mainclass} classinstances[next(rectanglename)] = mainclass classinstances["rectangle1"].temp = 10 print classinstances["rectangle1"].temp
if class going have x , y,
class mainclass: x, y = 0, 0 def getrectanglenamegenerator(n = 10): x = 0 while x <= n: x += 1 yield "rectangle" + str(x) rectanglename = getrectanglenamegenerator() classinstances = {next(rectanglename) : mainclass} classinstances[next(rectanglename)] = mainclass classinstances["rectangle1"].x = 11 print classinstances["rectangle1"].x
Comments
Post a Comment