简体   繁体   中英

Call a function from within another function in Python

I'm starting to learn Python, but I'm having an issue with my code and was hoping someone could help. I have two functions, and I would like to call one function from the other. When I simply tried calling the function, it seemed to be ignored so I'm guessing it's an issue with how I have called it. Below is the snippet of my code in question.

# 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")

Edited. I am actually calling the raw() function, it was just below the snippet. :)

Try simpler case like this:

def func2(msg):
    return 'result of func2("' + func1(msg) + '")'

def func1(msg):
    return 'result of func1("' + msg + '")'

print func1('test')
print func2('test')

It prints:

result of func1("test")
result of func2("result of func1("test")")

Notice the order of function definitions is intentionally reversed. The order of function definitions does not matter in Python.

You should specify better, what does not work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM