在没有 exec/eval 的情况下调用字符串中的代码,python
我有这样的代码,当玩家尝试吃东西时执行:
def eat(target='object'):
global current_room
global locations
global inventory
if target in inventory:
items[target]['on_eat'] #This is showing no results.
else:
print 'You have no ' + target + ' to eat.'
以及 items(trimmed) 的这段代码
items = {
'strawberry': {
'weight': 1,
'text': 'The strawberry is red',
'on_eat': "normal_eat('strawberry', 'pretty good, but not as sweet as you expected')"
},
'trees': {
'weight': 50,
'text': 'The trees are tall with large, leaf filled branches blocking out a majority of sunlight.',
'on_eat': "forcesay('Eating trees? What the hell is your problem?')"
}
}
是否有一种有效的方法来调用 items[whatever]['on_eat'] 而不做像 exec() 或 eval() 这样的愚蠢的事情?如果没有,替代格式作为示例也将受到赞赏。
在此之前, items[everyitems]['on_eat'] 值不是字符串,而是在代码运行后立即为每个项目执行 on_eat 。
我见过很多类似问题的答案,但它们不处理独特函数的参数 - 更好地说,它们更像 这个
I have this code that executes when a player attempts to eat something:
def eat(target='object'):
global current_room
global locations
global inventory
if target in inventory:
items[target]['on_eat'] #This is showing no results.
else:
print 'You have no ' + target + ' to eat.'
and this code for items(trimmed)
items = {
'strawberry': {
'weight': 1,
'text': 'The strawberry is red',
'on_eat': "normal_eat('strawberry', 'pretty good, but not as sweet as you expected')"
},
'trees': {
'weight': 50,
'text': 'The trees are tall with large, leaf filled branches blocking out a majority of sunlight.',
'on_eat': "forcesay('Eating trees? What the hell is your problem?')"
}
}
Is there a valid way of calling items[whatever]['on_eat'] without doing something silly like exec() or eval()? If not, alternative formatting as an example would also be appreciated.
Before this the items[everyitems]['on_eat'] values were not strings, but that executed the on_eat for every item as soon as the code was ran.
I have seen many answers to similar questions, but they don't deal with arguments for functions unique- to better put that, they were more like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将函数和函数参数存储为
partial
:You can store your function and function arguments as a
partial
:你可以使用代码模块
you can use the code module
部分函数的替代方法是编写这样的项目
并像这样调用它
您不需要那些
global
语句,除非您将重新分配它们An alternative to partial functions is to write items like this
and call it like this
You don't need those
global
statements there unless you will be reassigning them