Monday, 5 June 2017

python - Why does it do this ? if - __name__ == '__main__'





Duplicate of:
What does if __name__== "__main__" do?







Consider this code:



if __name__ == '__main__':
import pdb
pdb.run("interact()\n")


What does the following line mean?




if(__name__=='__main__')


I fainted.


Answer



This will be true if this module is being run as a standalone program. That way, something can act either as a module imported by another program, or a standalone program, but only execute the code in the if statement if executed as a program.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...