+ 2
def if_name_=="_main_". What does this mean?
print("before import") import math print("before functionA") def functionA(): print("Function A") print("before functionB") def functionB(): print("Function B {}".format(math.sqrt(100))) print("before __name__ guard") if __name__ == '__main__': functionA() functionB() print("after __name__ guard")
2 Réponses
+ 2
When the program is runing by itself and it's not imported as a module __name__=='__main__'
(It's actually two underlines)
But when it's imported as a module, __name__ is module's filename.
It's can be so useful when you want to write a code that can run by itself and you want use its functions in other codes, too.
+ 1
Thanks