Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of functions in a Python script matter?

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?

like image 583
flpn Avatar asked Jun 07 '17 22:06

flpn


People also ask

Does Python treats its functions as higher order?

Python too supports the concepts of higher order functions. Properties of higher-order functions: A function is an instance of the Object type. You can store the function in a variable.

Do functions need to be at the top Python?

Although in Python you can call the function at the bottom of your program and it will run (as we have done in the examples above), many programming languages (like C++ and Java) require a main function in order to execute.

What is the order function in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.

Is Python code executed sequentially?

As Python is an interpreted language, it follows a top-down approach. Just because python is interpreted there is no static entry point to the program and the source code is executed sequentially and it doesn't call any methods unless you manually call it.


2 Answers

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

like image 107
Dimitris Fasarakis Hilliard Avatar answered Oct 19 '22 03:10

Dimitris Fasarakis Hilliard


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
like image 24
MSeifert Avatar answered Oct 19 '22 04:10

MSeifert