Python-将对象用作函数的参数
我在循环中有一个函数,它接受5个参数,保存它们并反复使用它们的新值。但它看起来很混乱,我想知道是否可以为这些变量创建一个类,并在函数中使用一个对象作为参数Python-将对象用作函数的参数,python,Python,我在循环中有一个函数,它接受5个参数,保存它们并反复使用它们的新值。但它看起来很混乱,我想知道是否可以为这些变量创建一个类,并在函数中使用一个对象作为参数 while encounter: player, minion, monster, arrow, encounter = fight(player, minion, monster, arrow, encounter) 可能吗?如果是,我该怎么做?谢谢大家! 如果我理解你的意图,你可以尝试使用 然后,您可以将单个对象传递给函数,接收
while encounter:
player, minion, monster, arrow, encounter = fight(player, minion, monster, arrow, encounter)
可能吗?如果是,我该怎么做?谢谢大家! 如果我理解你的意图,你可以尝试使用 然后,您可以将单个对象传递给函数,接收函数的外观如下:
def testFunc(characterObject):
#Print the value of the "player" key
print(characterObject["player"])
#call some other function
otherFunc(characterObject["arrow"])
testFunc(characterObject)
是的,使用这五个属性和方法
fight()
初始化。您可以创建一个类,但这里不是教程的地方。如果您访问python.org网站,文档部分可能会有一个教程来帮助您入门。如果您需要在存储数据方面有很大的灵活性,可以使用dict
。将数据存储在一个文档中或创建一个新类。如果你不知道如何做到这一点,请查看一些教程。从这个开始:
def testFunc(characterObject):
#Print the value of the "player" key
print(characterObject["player"])
#call some other function
otherFunc(characterObject["arrow"])
testFunc(characterObject)