def if_name_=="_main_". What does this mean? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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")

6th Jun 2019, 3:07 PM
Ranga Sai
2 Antworten
+ 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.
6th Jun 2019, 3:20 PM
ΛM!N
ΛM!N - avatar
+ 1
Thanks
6th Jun 2019, 3:22 PM
Ranga Sai