I am learning python3 and I am learning import statement.
I have two python files.
In fiboo.py
I have following codes:
x=10
y=x+15
print("hello from fiboo")
def hello():
print("i am ashwin")
In main.py
I have following codes:
from fiboo import y
print("hello from main")
print(y)
I want to access only the y variable.So,I did from fiboo import y. But I see that 'hello from fiboo' also being printed.Will all the codes get runned even if I import specific variables from modules? While running the above code I see output as:
[Running] python -u "d:\python practise\main.py"
hello from fiboo
hello from main
25
Again I changed the code as:
import fiboo
print("hello from main")
print(fiboo.y)
Here also I see output as:
Running] python -u "d:\python practise\main.py"
hello from fiboo
hello from main
25
Does python compiles the whole code of modules even if we use from module import y
kind of statement? Doesn't it only import y
variable from fiboo.py
file while I use from module import y
? Where is the optimization then if the whole code gets compiled if we only want variable y from another module? Or it is the rules that whole modules code gets compiled and printed no matter if we use from fiboo import y
or import fiboo
?