I am learning main
and explore its applications with codes:
a = 1
b = 2
def main():
x = add(a, b)
print(x)
if __name__ == "__main__":
main()
def add(a, b):
a = a + 1
return a + b
However, it report NameError:
In [87]: run test.py
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
NameError: name 'add' is not defined
Re-position if __name__ == "__main__":
to end is a solution,
a = 1
b = 2
def main():
x = add(a, b)
print(x)
def add(a, b):
a = a + 1
return a + b
if __name__ == "__main__":
main()
In [88]: run test.py
4
I am confused about why the previous case failed.
if __name__ == "__main__":
invoke main
, main
invoke add.