2012-07-23 85 views
4

我開始學習Python,但我遇到了一個與我的代碼有關的問題,並希望有人能夠提供幫助。我有兩個函數,我想從另一個函數中調用一個函數。當我簡單地嘗試調用函數時,它似乎被忽略了,所以我猜這是我如何調用它的問題。下面是我的代碼片段。從Python中的另一個函數調用函數

# Define the raw message function 
def raw(msg): 
    s.send(msg+'\r\n') 

    # This is the part where I try to call the output function, but it 
    # does not seem to work. 
    output('msg', '[==>] '+msg) 

    return 

# Define the output and error function 
def output(type, msg): 
    if ((type == 'msg') & (debug == 1)) | (type != msg): 
     print('['+strftime("%H:%M:%S", gmtime())+'] ['+type.upper()+'] '+msg) 
    if type.lower() == 'fatal': 
     sys.exit() 
    return 

# I will still need to call the output() function from outside a 
# function as well. When I specified a static method for output(), 
# calling output() outside a function (like below) didn't seem to work. 
output('notice', 'Script started') 

raw("NICK :PythonBot") 

編輯。我實際上調用了raw()函數,它只是在代碼片段之下。 :)

+3

你確定你打算在那裏使用'&'和'|'嗎? – 2012-07-23 22:04:21

+0

「當我爲output()指定一個靜態方法」...等等,什麼? – 2012-07-23 22:05:47

+1

你根本就沒有調用第一個函數...... – 2012-07-23 22:11:55

回答

6

嘗試簡單的情況是這樣的:

def func2(msg): 
    return 'result of func2("' + func1(msg) + '")' 

def func1(msg): 
    return 'result of func1("' + msg + '")' 

print func1('test') 
print func2('test') 

它打印:函數定義

result of func1("test") 
result of func2("result of func1("test")") 

注意順序顛倒故意。函數定義的順序在Python中並不重要。

你應該指定更好,什麼不適合你。

+0

感謝您提供示例:) – 2012-07-23 22:48:48

+0

檢查您的原始代碼。正如伊格納西奧提到的那樣,'&'和'|'的含義與你期望的不同。 Python使用'和'和'or'作爲布爾表達式。 – pepr 2012-07-24 06:18:29

相關問題