Dead-i
Dead-i

Reputation: 71

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. :)

Upvotes: 6

Views: 65876

Answers (1)

pepr
pepr

Reputation: 20752

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.

Upvotes: 13

Related Questions