Reputation: 5451
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():
print("testing main")
if __name__ == "__main__":
hello()
**output :**
hello world
testing main
NOTE : I am aware of usage of __name__ == "__main__".
Example 2:
python()
def python():
print("testing main")
**output**
File "main_flow.py", line 2, in <module>
python()
NameError: name 'python' is not defined
As far as I know python executes the program in sequential manner(correct me if I am wrong), hence in Example 2 it is not able to find the method python() when it encounters it.
My confusion is why in example 1 the similar error has not occured, what is the flow of execution in this scenario.
Upvotes: 3
Views: 6562
Reputation: 2813
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. def hello(): # Python now knows about function hello()
5. print("hello world")
6. python()
2. def python(): # 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
- def python(): # Python doesn't reach this line because of the above error
- print("testing main")
The Example 2 solution would be:
1. def python(): # 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.
Upvotes: 7
Reputation: 152795
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 python
after 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> in hello()
1 def hello():
2 print("hello world")
----> 3 python()
4
5 hello()
NameError: name 'python' is not defined
Upvotes: 2