Don't get you??
If you name your file that way, then yes it is a file. If you don't name a file like that, then no, it isn't a file.
You mean in the context of if __name__ == "__main__"? If so, __name__ is a variable that all python programs have, if you run your python file directly from the interpreter (e.g. if you do something like $ python my_script.py) then the value of __name__ for my_script will be "__main__", but ONLY if you execute that file directly like $ python my_script.py.
But when we import smth isn't it being runned by python?
I mean the code which we import
Then, __name__ is assigned the fully qualified name of the module it is defined in, most cases the file name of the module (the file with .py extension) that is imported
I knew it , but when we run something that is imported then it also runs directly from interpreter ,does it?
You can only run or import a python module, not both at the same time. If you were to have: import module1 module1.func() inside module2.py and then run module2.py, you'd have run module2 and imported module1. You can run module1 directly, but it wouldn't be imported (you can't import a module inside itself)
You sad if a module runs directly from interpreter then it is name is main So when i import module 1 to module 2 ,then module 1runs directly from interpreter but it is name is not main
When you import module 1 into module 2 and you run module 2, module 1 is not run directly, but module 2 is. In this case module 2 __name__ is __main__, but module 1 __name__ is not.
U confused me a bit by saying "runs directly from interpreter"
I thought when we import it copies the code from module 1 to module2
Обсуждают сегодня