PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

[FIXED] Why would the top print() statement print to the console after a later line of code

 August 15, 2022     output, python     No comments   

Issue

code output

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing