2010-08-11 7 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?')" 
    } 
} 

निष्पादन() या eval() जैसे मूर्खतापूर्ण कुछ किए बिना आइटम [जो भी] ['on_eat'] को कॉल करने का एक वैध तरीका है? यदि नहीं, तो उदाहरण के रूप में वैकल्पिक स्वरूपण की भी सराहना की जाएगी।

इससे पहले आइटम [हरिटम्स] ['on_eat'] मान तार नहीं थे, लेकिन कोड समाप्त होने के तुरंत बाद प्रत्येक आइटम के लिए on_eat निष्पादित किया गया।

मैं इसी तरह के सवाल करने के लिए कई जवाब देखा है, लेकिन वे तर्क के साथ सौदा नहीं है कार्यों बेहतर है कि डाल करने के लिए unique- के लिए, वे और अधिक 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

कि अजीब है फिर नियत किया जाएगा वहाँ जो global बयान जरूरत नहीं है, यह काम करता हैं। खैर मुझे लगता है कि यह समझ में आता है, क्या आप/कोई मुझे बता सकता है कि यह कैसे गति/सीपीयू/कदमवार नीचे चला जाता है? साथ ही, आंशिक रूप से आइटमों को परिभाषित करने के बाद आंशिक रूप से निष्पादित क्यों नहीं होते हैं जब आइटम [blah] ['on_eat'] मान कम/सामान्य फ़ंक्शन कॉल होते हैं? –

+0

क्योंकि आंशिक वास्तव में * कॉल * फ़ंक्शन नहीं करता है जब आंशिक आपके आइटम के on_eat तत्वों में आंशिक रूप से बनाया जाता है। यह सब कुछ कहने के लिए किया जाता है और इसे कॉल करने के लिए कोई तर्क है। जब आप आंशिक तक पहुंचते हैं और इसे() के साथ कॉल करते हैं तो फ़ंक्शन को कॉल किया जाता है। इसे आज़माएं: z = आंशिक (मिनट, 2, 4, 6) कुछ भी नहीं होता है। अब z को कॉल करें: z() आप उत्तर वापस प्राप्त करते हैं 2. आंशिक बनाया गया था जब मिनट नहीं कहा गया था; इसे केवल तब कहा जाता था जब आपने आंशिक रूप से z() के रूप में आविष्कार किया था। स्पीडवाइव, यह मूल कार्य को कॉल करने के समान ही होना चाहिए। – PaulMcG

+0

धन्यवाद। इसके अलावा, अगर मैं वस्तुओं के साथ एक साथ कई चीजें करना चाहता हूं ['दांते \' इन्फर्नो से जादुई परिवहन भोजन] ['on_eat'] प्रिंट करता है, प्रिंट करता है, वर्तमान_रूम बदलता है, सामान्य_एट कॉल करता है, फिर से प्रिंट करता है, उपयोगकर्ता स्पीकर विस्फोट करता है, ect। मुझे लगता है कि मुझे एक अलग कार्य करना होगा जो यह सब करता है? –

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

+2

ऑब्जेक्ट के बाद एक समापन उद्धरण याद आया, फिर इसे संपादित करें! – aaronasterling

0

आंशिक कार्यों के लिए एक वैकल्पिक lik आइटम लिखने के लिए उपयोग कर सकते हैं ई इस

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.' 

की तरह फोन आप जब तक आप उन्हें

संबंधित मुद्दे