Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format Q
#2
The first one will run slightly slower, because it does more logic. That's not really important though, as the time cost is going to be absurdly small, and paid infrequently.

The difference between the codes is that if you import the first one, the if-block won't run. That's desirable if you're trying to write a script which you want to be both runnable and importable. I often start my code with this template:
#!/usr/bin/env python3

def main():
    pass

if __name__ == "__main__":
    main()
Typically I write everything in the main function, unless I'm iterating a lot on a script in which case I omit the main function so that I can use python -i myscript.py and examine the variables.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply


Messages In This Thread
Format Q - by ebolisa - Jan-16-2020, 05:32 PM
RE: Format Q - by micseydel - Jan-16-2020, 06:05 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020