Python 嵌套在方法中的字典在调用外部方法时自动执行所有值(方法)
Python dictionary nested within method auto-executes all values (methods) when outer method is called
我正在为一款游戏制作一个简单的骨架,为了尝试变得更好 "pythonic",我正在使用 objects/classes/dictionaries 来尝试捕捉我所有的 [=26] =](作为函数之上的方法等)。
出于某种原因,每次我在 class "Player" 中执行方法 'act' 时,嵌入在 act 中的字典会运行它的所有值(反过来, class "Player" 的同一实例中的方法)。换句话说,玩家每次都选择 "attack, heal, and flee",在提示之前一次全部选择。
我确定有一个简单的解释,但我已经找了好几个小时了,找不到另一个例子来说明某人的字典中自动 运行 嵌入了所有方法。你能帮忙吗?
谢谢!
-杰克
from random import randint
### BEGIN ALL CLASSES HERE
# To be used for all game objects (living and non-living)
class gameObject(object):
def __init__(self, name):
self.name = name
# To be used for all characters who can act in some way/be killed/change
class livingThing(gameObject):
def __init__(self, name, HP=1):
self.name = name
self.HP = HP
# The playable character(s)
class Player(livingThing):
def __init__(self,name="The Stranger", HP=4, MP=5, strength=1, intellect=1, spirit=1, luck=5, gil=6):
self.name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.luck = luck
def act(player, enemy):
actions = {
"attack" : player.attack(enemy),
"heal" : player.heal(enemy),
"flee" : player.flee()
}
#Takes input from the player
decision = input("What would you like to do? ")
if decision.lower() in actions:
actions[decision.lower()]
else:
print("That didn't work! Try again.")
# Prints both player and enemy HP
def printHP(player, enemy):
print("{0}'s' HP: {1} \n{2}'s HP: {3}".format(player.name, player.HP, enemy.name, enemy.HP))
# Allows the player to attack an enemy (currently functional)
def attack(player, enemy):
enemy.HP -= player.strength
print("You strike {0} for {1} damage!".format(enemy.name, player.strength))
player.printHP(enemy)
# Allows the player to heal a certain amount of health based on its "spirit" stat (currently functional)
def heal(player, enemy):
healed = randint(0, player.spirit)
player.HP += healed
print("You've healed for {0}!".format(healed))
player.printHP(enemy)
#Allows the player to attempt to run away
def flee(player):
randluck = randint(0, player.luck)
if randluck > 3:
print("You successfully escaped!")
return player.HP
else:
print("You weren't able to escape!")
# Anything that can act with/against the player
class Actor(livingThing):
def __init__(self, name="Unknown Entity", HP=10, MP=2, gil=3):
self. name = name
self.HP = HP
self.MP = MP
self.gil = gil
### END ALL CLASSES ###
### DICTIONARIES CONTAINING ACTIONS ###
### CHARACTERS ###
fighter = Player()
monster = Actor()
fighter.act(monster)
我明白了。当您执行 Python 代码,并且您有一个字典时,Python 会对字典进行完整计算。如果您希望您的值(在您的 key:value 中)对是这些方法的 结果 ,这无疑是一种方法。
在您的情况下,您可以做的是引用 函数 本身,而不是调用它。你可以通过去掉括号来做到这一点,像这样:
player.attack
而不是
player.attack()
然后,要调用该函数,您可以执行类似
的操作
actions[decision.lower()](enemy)
因为你的函数之一,flee,不接受任何参数,你可以给 flee 一个你不需要的参数' 在函数中使用。如果您正在设计玩家可以 执行 的许多方法,那么一种策略是只为它们提供命名参数,如下所示:
def f1(enemy=None,something=None,foo=None):
if enemy is None:
raise Exception("enemy cannot be None")
#process_enemy
但是,如果您还有大量参数,那么您可以这样做:
def attack(**kwargs):
#kwargs is a dictionary of parameters provided to the function
enemy = kwargs.get('enemy',None)
if enemy is None:
raise Exception("enemy cannot be None")
def eat(**kwargs):
food = kwargs.get('food',None)
if enemy is None:
raise Exception("food cannot be None")
attack(enemy="someenemyobject")
eat(food="somefoodobject")
attack() # raises Exception
attack(food="somefoodobject") # raises Exception
food(enemy="someenemyobject") # raises Exception
food(food="somefoodobject",enemy="someenemyobject") # does not raise Exception
我正在为一款游戏制作一个简单的骨架,为了尝试变得更好 "pythonic",我正在使用 objects/classes/dictionaries 来尝试捕捉我所有的 [=26] =](作为函数之上的方法等)。
出于某种原因,每次我在 class "Player" 中执行方法 'act' 时,嵌入在 act 中的字典会运行它的所有值(反过来, class "Player" 的同一实例中的方法)。换句话说,玩家每次都选择 "attack, heal, and flee",在提示之前一次全部选择。
我确定有一个简单的解释,但我已经找了好几个小时了,找不到另一个例子来说明某人的字典中自动 运行 嵌入了所有方法。你能帮忙吗?
谢谢! -杰克
from random import randint
### BEGIN ALL CLASSES HERE
# To be used for all game objects (living and non-living)
class gameObject(object):
def __init__(self, name):
self.name = name
# To be used for all characters who can act in some way/be killed/change
class livingThing(gameObject):
def __init__(self, name, HP=1):
self.name = name
self.HP = HP
# The playable character(s)
class Player(livingThing):
def __init__(self,name="The Stranger", HP=4, MP=5, strength=1, intellect=1, spirit=1, luck=5, gil=6):
self.name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.luck = luck
def act(player, enemy):
actions = {
"attack" : player.attack(enemy),
"heal" : player.heal(enemy),
"flee" : player.flee()
}
#Takes input from the player
decision = input("What would you like to do? ")
if decision.lower() in actions:
actions[decision.lower()]
else:
print("That didn't work! Try again.")
# Prints both player and enemy HP
def printHP(player, enemy):
print("{0}'s' HP: {1} \n{2}'s HP: {3}".format(player.name, player.HP, enemy.name, enemy.HP))
# Allows the player to attack an enemy (currently functional)
def attack(player, enemy):
enemy.HP -= player.strength
print("You strike {0} for {1} damage!".format(enemy.name, player.strength))
player.printHP(enemy)
# Allows the player to heal a certain amount of health based on its "spirit" stat (currently functional)
def heal(player, enemy):
healed = randint(0, player.spirit)
player.HP += healed
print("You've healed for {0}!".format(healed))
player.printHP(enemy)
#Allows the player to attempt to run away
def flee(player):
randluck = randint(0, player.luck)
if randluck > 3:
print("You successfully escaped!")
return player.HP
else:
print("You weren't able to escape!")
# Anything that can act with/against the player
class Actor(livingThing):
def __init__(self, name="Unknown Entity", HP=10, MP=2, gil=3):
self. name = name
self.HP = HP
self.MP = MP
self.gil = gil
### END ALL CLASSES ###
### DICTIONARIES CONTAINING ACTIONS ###
### CHARACTERS ###
fighter = Player()
monster = Actor()
fighter.act(monster)
我明白了。当您执行 Python 代码,并且您有一个字典时,Python 会对字典进行完整计算。如果您希望您的值(在您的 key:value 中)对是这些方法的 结果 ,这无疑是一种方法。
在您的情况下,您可以做的是引用 函数 本身,而不是调用它。你可以通过去掉括号来做到这一点,像这样:
player.attack
而不是
player.attack()
然后,要调用该函数,您可以执行类似
的操作actions[decision.lower()](enemy)
因为你的函数之一,flee,不接受任何参数,你可以给 flee 一个你不需要的参数' 在函数中使用。如果您正在设计玩家可以 执行 的许多方法,那么一种策略是只为它们提供命名参数,如下所示:
def f1(enemy=None,something=None,foo=None):
if enemy is None:
raise Exception("enemy cannot be None")
#process_enemy
但是,如果您还有大量参数,那么您可以这样做:
def attack(**kwargs):
#kwargs is a dictionary of parameters provided to the function
enemy = kwargs.get('enemy',None)
if enemy is None:
raise Exception("enemy cannot be None")
def eat(**kwargs):
food = kwargs.get('food',None)
if enemy is None:
raise Exception("food cannot be None")
attack(enemy="someenemyobject")
eat(food="somefoodobject")
attack() # raises Exception
attack(food="somefoodobject") # raises Exception
food(enemy="someenemyobject") # raises Exception
food(food="somefoodobject",enemy="someenemyobject") # does not raise Exception