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

最佳答案

尝试这样更简单的情况:

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

您应该指定更好的方法,哪些方法对您不起作用。

10-05 20:49
查看更多