72

Let's say I have two functions in my script: sum_numbers and print_sum. Their implementation is like this:

def sum_numbers(a, b):
    return a + b

def print_sum(a, b):
    print(sum_numbers(a, b))

So my question is: does the order in which the function are written matter? If I had written the print_sum function first and then the sum_numbers, would the code still work? If the answer is yes, does it always work?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
flpn
  • 1,868
  • 2
  • 19
  • 31
  • 7
    "*... would the code still work?*" <-- it's literally faster to just try this part than ask about it. Whenever you want to ask "*does thing work in python?*", try it first. If the result baffles you, you'll be able to ask a more interesting and specific question. – Andras Deak -- Слава Україні Jun 08 '17 at 10:24
  • 28
    @AndrasDeak The question may be simple, but that doesn't mean it's not worth asking. Questions like these can be helpful for newer programmers, or for when the answer can't otherwise be found from a quick search. – Stevoisiak Mar 22 '18 at 18:35
  • 3
    @StevenVascellaro I'm not sure I understand your point. New programmers can also literally try it faster than ask or read about it :) – Andras Deak -- Слава Україні Mar 22 '18 at 18:40
  • ...or where a user is wondering and not before a live python interpreter to actually try anything out. However I was wondering abbot the mental ergonomics aspect of this question, I know a log of legacy C++ static typed code somewhat artificially pushed leaf call stack dependency order to the top of file but when learning a codebase it may be best to order the functions for top down execution. With advanced IDEs capable of jumping to arbitrary positions it may not matter as much as it once did however. – jxramos May 01 '18 at 17:24
  • 2
    @AndrasDeak However people that reach this question doesn't have exactly this code snippet in mind. Also note that the question actually consists of two where the second is not as simple to check as the first. Also note that in general this type of question is not possible to check if there's input involved (that would in general need to solve the stopping problem). – skyking May 30 '19 at 07:38

3 Answers3

112

The only thing that Python cares about is that the name is defined when it is actually looked up. That's all.

In your case, this is just fine, order doesn't really matter since you are just defining two functions. That is, you are just introducing two new names, no look-ups.

Now, if you called one of these (in effect, performed a look-up) and switched the order around:

def print_sum(a, b):
    print(sum_numbers(a, b))

print_sum(2, 4)

def sum_numbers(a, b):
    return a + b

you'd be in trouble (NameError) because it will try to find a name (sum_numbers) that just doesn't exist yet.

So in general, yes, the order does matter; there's no hoisting of names in Python like there is in other languages (e.g JavaScript).

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
34

It doesn't matter in which order the functions are created. It only matters when the call to the function is done:

def print_sum(a, b):
    print(sum_numbers(a, b))

def sum_numbers(a, b):
    return a + b

print_sum(1, 3)
# 4

that works because at the time print_sum is called both functions do exist. However if you call the function before defining sum_numbers it would fail because sum_numbers isn't defined yet:

def print_sum(a, b):
    print(sum_numbers(a, b))

print_sum(1, 3)

def sum_numbers(a, b):
    return a + b

throws:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-37c0e3733861> in <module>()
      2     print(sum_numbers(a, b))
      3 
----> 4 print_sum(1, 3)
      5 
      6 def sum_numbers(a, b):

<ipython-input-34-37c0e3733861> in print_sum(a, b)
      1 def print_sum(a, b):
----> 2     print(sum_numbers(a, b))
      3 
      4 print_sum(1, 3)
      5 

NameError: name 'sum_numbers' is not defined
MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • 5
    To be nitpicking there actually are cases where the order matters. If a symbol is being looked up as part of the definition/creation of the object the symbol must be defined earlier. An easy example is if you've got default argument of a function, those are evaluated at definition time). Another example would be inheritance where the base classes must be defined first. – skyking May 30 '19 at 07:46
  • Why not making an answer from your comment @skyking ? – Alexandre Huat Feb 16 '21 at 09:09
1

It doesn't matter in which order functions are defined as shown below:

def display():
    print(text())

def text():
    return "Hello World"

display() # "Hello World" is displayed

But, it matters where functions are called so if calling "display()" at the beginning as shown below:

display() # Error

def display():
    print(text())

def text():
    return "Hello World"

Then, there is the error as shown below:

Traceback (most recent call last): File "", line 1, in NameError: name 'display' is not defined

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129