this post was submitted on 28 May 2025
729 points (96.3% liked)

Programmer Humor

24149 readers
1873 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Also, do y'all call main() in the if block or do you just put the code you want to run in the if block?

you are viewing a single comment's thread
view the rest of the comments
[–] HiddenLayer555@lemmy.ml 74 points 2 weeks ago* (last edited 2 weeks ago) (2 children)

Looks at all the Python scripts in my bin folder that I wrote.

[–] 30p87@feddit.org 64 points 2 weeks ago* (last edited 2 weeks ago) (3 children)

Never heard of

def main():
    pass

if __name__ == '__main__':
    main()

?

[–] grrgyle@slrpnk.net 49 points 2 weeks ago (3 children)

I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af

[–] frezik@midwest.social 9 points 2 weeks ago

Python: I'm so readable that I'm practically executable pseudo-code

Also Python: if __name__ == '__main__': . . .

[–] bane_killgrind@slrpnk.net 6 points 2 weeks ago (1 children)

Now think about this, you have logic that doesn't make sense when run directly, but you need it to be a library.

You have multiple name=main statements in some of your functions

[–] grrgyle@slrpnk.net 3 points 2 weeks ago (1 children)

I'm not sure I'm following the implication. Name=main is for scripts primary, is it not?

I've never thought to add more than one of these conditionals anyway...

[–] bane_killgrind@slrpnk.net 2 points 2 weeks ago

So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers.

So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script.

[–] Anomalocaris@lemm.ee 3 points 2 weeks ago (3 children)

I still wonder why.

unless it's for something that you want to work as an importable module and a standalone tool, then why do you need that?

[–] Archr@lemmy.world 3 points 2 weeks ago

The main two reasons that I can think of to include this even when you have no intention of importing this as a library are:

  1. For unit testing you will need to import as a module.
  2. Sometimes I will run a python interactive interpreter and then import my script so that I can do some manual testing without needing to change my main function or if stmt.
[–] nickwitha_k 1 points 2 weeks ago* (last edited 2 weeks ago)

This is exactly why the conditional is used. It allows the script to function both as a standalone application and a library.

ETA: Probably would make sense to just treat it as default behavior in the interpreter and only require the conditional to overwrite in cases where main is not the main function and/or pre-processing is needed.

[–] grrgyle@slrpnk.net 1 points 2 weeks ago

Oh that is a good point actually. It's been a while since I have done any serious Python, so I'm not sure why you couldn't just use convention instead of this conditional.

For my part, if a Python script is meant to be executed, then I'll give it a shebang, drop the .py, and simply mark it as executable in the filesystem. 🤷‍♂️

[–] HiddenLayer555@lemmy.ml 24 points 2 weeks ago* (last edited 2 weeks ago)

Heard of it, was too lazy to do it that way.

To be fair I now do it that way, but not when I was learning Python.

[–] firelizzard@programming.dev 4 points 2 weeks ago (1 children)
[–] 30p87@feddit.org 7 points 2 weeks ago (2 children)

Not having tons of code in one if statement, but in a function.

[–] IronKrill@lemmy.ca 12 points 2 weeks ago

And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function.

[–] firelizzard@programming.dev 2 points 2 weeks ago (1 children)

I thought you were saying to literally use def main(): pass, that’s why I was confused

[–] 30p87@feddit.org 1 points 2 weeks ago

Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder

[–] Anomalocaris@lemm.ee 11 points 2 weeks ago (1 children)

custom bin folders are a realm no God dares to tread

[–] SubArcticTundra@lemmy.ml 2 points 2 weeks ago* (last edited 2 weeks ago)

I can and I do

*trollface*