6

Python の学習を始めていますが、コードに問題があり、誰かが助けてくれることを望んでいました。2 つの関数があり、1 つの関数を別の関数から呼び出したいと考えています。単純に関数を呼び出そうとしたところ、無視されたように見えたので、呼び出し方の問題だと思います。以下は、問題の私のコードのスニペットです。

# 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 に答える