从Python中的另一个函数中调用函数

时间:2012-07-23 22:02:55

标签: python function

我开始学习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()函数,它只是在片段下面。 :)

1 个答案:

答案 0 :(得分:10)

尝试这样简单的案例:

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中无关紧要。

你应该更好地指定,什么对你不起作用。