class Item(object):
def __init__(self, quantity, name, description, effect): #effect is in code and used by the computer, not shown to the player.
self.quantity=quantity
self.name=name
self.description=description
self.effect=effect
def useitem(Item):
if Item.quantity>0:
Item.quantity-=1
Item.effect
我想使它在使用Item
時會觸發某個函數,但我不知道該怎麼做。我正在尋找的東西沿線如何將自定義函數添加到每個對象不同的類中?
bandaid=Item(1, "band-aid", "helps with cuts", health+=20)
但當然,這是行不通的。 我該怎麼做?
我的編碼知識非常有限,所以如果你能解釋你到底在做什麼,那將是非常好的。
閱讀關於'lambda'的文檔。此外,您還可以使用函數(不帶'()')作爲參數,因爲它們是Python中所有其他對象。 –
只需注意提高可讀性:在操作符之前和之後(例如' - ='),應該有一個空格。 [請參閱空間的PEP 8指南](https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements) – Worm