6

我开始学习 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() 函数,它就在代码片段的下方。:)

4

1 回答 1

13

尝试像这样更简单的情况:

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 中函数定义的顺序无关紧要。

你应该更好地说明什么对你不起作用。

于 2012-07-23T22:23:12.103 回答