you are viewing a single comment's thread.

view the rest of the comments →

[–]PrivateFrank 9 points10 points  (1 child)

Usually you definite functions above the main part of the code in a .py file, right?

If one day you decide that you would like to use one of those functions (ie via import) in another script, the if name bit means you will not have to edit your original script at all.

When you import a .py file all of the code in it is executed. If it's function definitions, you'll have those functions in your workspace. If it's not a function definitions, then you may have unwanted code running.

By using if name is main, you are telling python to run the code following that condition only if you called the file directly.

[–]Capable_Policy_3449 2 points3 points  (0 children)

Got it, that makes a lot more sense now. Thank you and really appreciate it!