Skip to content Skip to sidebar Skip to footer

Understanding The Execution Flow In Python

Being new to python, confused with the flow of execution: To elaborate I am stating the below examples: Example 1: def hello(): print('hello world') python() def python()

Solution 1:

Edit 1: When you call a custom function in python it must know where it is located in the file. We use def function_name(): to define the location of functions we use in our scripts. We must call def function_name(): before we call function_name(), otherwise the script won't know about function_name() and will raise an exception (function not found error).

By running def function_name(): it only lets the script know there is a function called function_name() but it doesn't actually run the code inside function_name() until you call it.

While in your second example you call python() before the script has reached def python(), so it doesn't know what python() is yet.

The Example 1 order is:

1.defhello(): # Python now knows about function hello()5.print("hello world")
6.        python()

2.defpython(): # Python now knows about function python()7.print("testing main")

3.if __name__ == "__main__":
4.       hello() 

The Example 2 order is:

1.    python()    # Error because Python doesn't know what function python() is yet

-     defpython(): # Python doesn't reach this line because of the above error
-         print("testing main")

The Example 2 solution would be:

1.defpython(): # Python now knows about function python()3.print("testing main")

2.     python()   

Edit 2: To reiterate Example 1 from the scripts point of view:

def hello(): 
def python(): 
if __name__ == "__main__":
hello() 
print("hello world")
python()
print("testing main")

This is the order the script will see and run each line of code. So clearly the script knows about python() as the def is called on line #2, and python() is called on line number #6.

It appears you do not understand what scope means when it comes to defines. Read up about it. The scope of a function is not executed during a def, it is only executed when the function is called.

Solution 2:

This is not about __name__ == '__main__' but because the function call in your second example is executed before the function is defined.

Your first example executes the function pythonafter all functions are defined because hello is called after the function definitions (so there is no name error)!

If you put hello() between the definitions you would get the error again:

def hello():
    print("hello world")
    python()

hello()

def python():
    print("testing main")

gives:

hello world 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-21db1c6bfff8> in <module>()
      3     python()
      4 
----> 5 hello()
      6 
      7 def python():

<ipython-input-7-21db1c6bfff8> inhello()
      1 def hello():
      2     print("hello world")
----> 3     python()
      4 
      5 hello()

NameError: name 'python' isnot defined

Post a Comment for "Understanding The Execution Flow In Python"