Issue
one.func() returns the output "top level one .py" and "one.py has been imported" which appears before the output line "top level two.py".
import one
print("top level two.py")
one.func()
if __name__ == '__main__':
print("two.py being run directly")
else:
print("two is being imported")'''
The one.py module is:
def func():
print("func in one.py")
print("top level one.py")
if __name__ == '__main__':
print("one.py is being run directly")
else:
print("one.py has been imported")
Solution
It's because you are importing one before printing "top level two.py". if one.py looks like this:
print("top level one.py")
def func():
#do something
if __name__ == '__main__':
print("one.py being run directly")
else:
print("one.py has been imported")
and with the two.py code above, then one.py is run first, when it is imported.
Because one is running first, then its print statements will happen before the prints in two, and so they will show up first.
Answered By - 2pichar Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.