Import a script python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Import a script python

Does the <<if __name__="__main__":>> , import a python script and if why does it swap the if and else conditioned statements

8th Jun 2017, 12:03 AM
thomo
thomo - avatar
1 Answer
+ 4
No. that statement does not import any script. it is a test case to see if the current script is being runned directly OR being imported. if it is being runned the special variable __name__ will have value '__main__', this means that what come after the if will be executed if it is being imported, what comes after the if will not be executed. Its a Python trick.
8th Jun 2017, 12:27 AM
Ulisses Cruz
Ulisses Cruz - avatar