6

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

18
  • 3
    Are you sure you meant to use & and | there? Commented Jul 23, 2012 at 22:04
  • 1
    You're not calling the first function at all.... Commented Jul 23, 2012 at 22:11
  • 1
    I'm grasping at straws here, but might it be the case that raw and output are names already used in Python? Try renaming the functions, and see if that changes anything. Commented Jul 23, 2012 at 22:12
  • 2
    You should post an example that is expected to work as is. This snippet is apparently cut out of some bigger code. The problems are probably caused by the code that is not presented here. Try simpler case with two functions to make sure that it works. Commented Jul 23, 2012 at 22:14
  • 1
    I'd be careful using type as a variable. It's a builtin function. You can override it, but may want to be careful not to do so. Commented Jul 24, 2012 at 0:06

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for providing an example :)
Check your original code. As Ignacio mentioned, the & and | means something else than you expect. Python uses and and or for boolean expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.