我想知道是否有办法生成类属性,方法是循环遍历init方法的参数,而不明确引用包含init方法的所有参数的列表?如何通过扩展__init__参数来创建实例属性?
在下面的例子中,我可以循环使用hp,image,speed,x,y来创建自我参数吗?
class Character(pygame.sprite.Sprite):
def __init__(self, hp, image, speed, x, y):
# Call the parent class (Sprite) constructor
super(Character, self).__init__()
self.image = image
self.rect = self.image.get_rect().move(x, y) #initial placement
self.speed = speed
self.hp = hp
例如用一个循环,看起来就像是:
class Character(pygame.sprite.Sprite):
def __init__(self, hp, image, speed, x, y):
# Call the parent class (Sprite) constructor
super(Character, self).__init__()
for arg in arguments:
self.arg = arg
我不太清楚如何让“论据”是指惠普,图像,速度,X和Y?或者我坚持使用下面的列表?
class Character(pygame.sprite.Sprite):
def __init__(self, hp, image, speed, x, y):
# Call the parent class (Sprite) constructor
super(Character, self).__init__()
for arg in [self, hp, image, speed, x, y]:
self.arg = arg
'给arg [自我,惠普,图像,速度,X,Y]:self.arg = arg'只会留下一个单一属性'ARG '用'y'的价值...... – schwobaseggl
@Sorade是有用的答案(如果是的话,请接受)?你解决了你的问题吗(请让其他人知道你是怎么做的)? – salomonderossi