I am creating a turn-based game, where the player can create units at any time during his/her turn.
A shortened version of the Unit class is:
class Unit(object):
def __init__(self, xpos, ypos, color = 'red'):
self.xpos = xpos
self.ypos = ypos
self.color = color
And I was wondering if there was a way to automate creation of instances of this class, without
hardcoding something like:
unit1 = Unit(1, 1)
unit2 = Unit(1, 2)
...
unitn = Unit(1, 2)
So that I could just make a function such as create_unit(xpos, ypos, color = 'red')
and I wouldn't have to worry about hardcoding every unit spot avaliable
Thanks in advance!
P.S. I'm using Python 2.7
create_unitfunction would do exactly the same thing theUnitconstructor already does, so what would be the point?nseparate variables, but one variable with a list ofnobjects—e.g.,units = [Unit(1, 2) for _ in range(n)]. See Keep data out of your variable names and Why you don't want to dynamically create variables.