2010-08-11 87 views
5

我有这样的代码,当玩家试图吃点东西执行:调用代码,蟒蛇

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 = { 
'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。

我见过很多答案类似的问题,但他们不带参数的处理的功能,独特 - 更好地投入,他们更像this

回答

6

您可以将您的函数和函数参数为partial

from functools import partial 

items = { 
'strawberry': { 
    'weight': 1, 
    'text': 'The strawberry is red', 
    'on_eat': partial(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': partial(forcesay, 'Eating trees? What the hell is your problem?') 
    } 

def eat(target='object'): 
    # those globals are probably not necessary 
    if target in inventory: 
     items[target]['on_eat']() #Add()'s to call the partial 
    else: 
     print 'You have no ' + target + ' to eat.' 
+0

这是奇怪的;有用。那么我想这是有道理的,你能/有人告诉我这是如何降低速度/ CPU /步骤明智?此外,为什么这些局部变量只是在项目被定义为随意地执行,比如项目[blah] ['on_eat']的值是“”较少/正常的函数调用? – 2010-08-11 06:27:23

+0

因为部分实际上不会在项目字典的on_eat元素中创建部分时调用函数。它所做的就是捕获要调用的函数以及调用它的任何参数。当你访问局部并用()调用它时,函数被调用。试试这个:z = partial(min,2,4,6)没有任何反应。现在打电话给z:z()你回到答案2. min在部分被创建时不被调用;它只在调用partial(z())时被调用。在速度上,它应该与调用原始函数非常接近。 – PaulMcG 2010-08-11 06:33:19

+0

谢谢。此外,如果我想同时用物品['Dante'的地狱'中的神奇交通食物]'['on_eat']打印,改变current_room,调用normal_eat,再次打印,爆炸用户说话者等等来做多件事情怎么办? 我假设我将不得不做一个单独的功能,完成所有这一切? – 2010-08-11 06:53:25

1

可以使用代码模块

def eat(target='object'): 
    import code 
    console = code.InteractiveConsole(locals()) # make a python interpreter with local vars 
    if target in inventory: 
     console.push("items[target]['on_eat']") 
    else: 
     print 'You have no ' + target + ' to eat.' 
+0

抱歉,我错过了对象 – user250418 2010-08-11 07:04:22

+2

后的结束报价,然后编辑它! – aaronasterling 2010-08-11 07:06:06

0

到部分功能的替代方法是写项LIK E本

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?',)) 
    } 
} 

,并调用它

def eat(target='object'): 
    if target in inventory: 
     func, args = items[target]['on_eat'] 
     func(*args) 
    else: 
     print 'You have no ' + target + ' to eat.' 

你不需要那些global声明那里,除非你将被重新分配他们