What")
When the Python interpeter module and assigns it to a variable called
math. This is equivalent to replacing
import math with block, creating a function object, then assigning that function object to a variable called
functionA.
It prints the string
"before functionB".
It executes the second
def block, creating another function object, then assigning it to a variable called
functionB.
It prints the string
"before __name__ guard".
Only When Your Module Is the Main Program
__name__was indeed set to
"__main__"and it calls the two functions, printing the strings
"Function A"and
"Function B 10.0".
Only When Your Module Is Imported by Another
__name__will be
"foo", not
"__main__", and it'll skip the body of the
ifstatement.
Always
 files.
Question: Can I have multiple
__name__ checking blocks? Answer: it's strange to do so, but the language won't stop you.
Suppose the following is in
foo2.py. What happens if you say
python foo2.py on the command-line? Why?
# Suppose this is foo2.py. def functionA(): print("a1") from foo2 import functionB print("a2") functionB() print("a3") def functionB(): print("b") print("t1") if __name__ == "__main__": print("m1") functionA() print("m2") print("t2")
__name__check in
foo3.py:
# Suppose this is foo3.py. def functionA(): print("a1") from foo3 import functionB print("a2") functionB() print("a3") def functionB(): print("b") print("t1") print("m1") functionA() print("m2") print("t2")
# Suppose this is in foo4.py __name__ = "__main__" def bar(): print("bar") print("before __name__ guard") if __name__ == "__main__": bar() print("after __name__ guard")__".