【发布时间】:2020-10-12 21:37:03
【问题描述】:
我正在尝试使用对象中的方法来选择性地更改该对象的特定属性。我一直无法弄清楚如何让该方法识别我传递给它的值作为指示预先存在的属性。
class Attributes(object):
def __init__(self, strength, speed):
self.strength = strength
self.speed = speed
def increase(self, attribute, amount):
self.attribute += amount
You = Attributes(1, 2)
You.increase(strength, 5)
print(You.strength)
控制台告诉我“强度未定义”。
将值返回给对象是我可能做错的另一件事,所以那里的任何建议也会有所帮助。谢谢
【问题讨论】:
标签: python object methods properties