2012-07-23 118 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

相关问题