Python3: Precedence Question

In python, I thought methods and functions had to be defined in the code before they were called. For example, we always define a subscriber callback function before we instantiate the subscriber (which uses the callback as a parameter).

However, in a recent project my callback function called several “helper functions” that were defined later in the code. The structure looked something like:

def callback(msg):
	help_function1()
	help_function2()
	help_function3()

def help_function1()
	...
def help_function2()
	...
def help_function3()
	...

...
sub = rospy.Subscriber("/foo", Bar, callback)

Because this worked without any error, I didn’t realize until afterward this confilicted with what I thought was a rule of python. Are there certain cases where this won’t work? Obviously variables have to be previously declared, but is this not true for functions?

Does anyone have any insight on this?

Ok, I think I have found the answer.

The short version is that all of your functions have to be declared before any outside code calls any of them. The order that the functions are in doesn’t matter as long as they all come first.

More info at

2 Likes