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

Programmer Humor

23784 readers
3088 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?

top 50 comments
sorted by: hot top controversial new old
[–] iAvicenna@lemmy.world 5 points 6 days ago* (last edited 6 days ago) (1 children)

wait till you see

if __name__ = "__main__":

   main()
`
[–] HiddenLayer555@lemmy.ml 4 points 6 days ago* (last edited 6 days ago) (1 children)

Luckily Python is one step ahead:

Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if __name__ = "__main__":
... 
...    main()
...    
    File "<python-input-0>", line 1
    if __name__ = "__main__":
        ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Also TIL that := is a thing in Python.

[–] iAvicenna@lemmy.world 1 points 6 days ago* (last edited 6 days ago)

yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.

and also := also known as the walrus operator is very fun and sometimes very convenient to use

[–] arschflugkoerper@feddit.org 88 points 1 week ago (6 children)

What kind of psychopath would put the code in the if block.

[–] HiddenLayer555@lemmy.ml 74 points 1 week ago* (last edited 1 week ago) (3 children)

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

[–] 30p87@feddit.org 64 points 1 week ago* (last edited 1 week ago) (7 children)

Never heard of

def main():
    pass

if __name__ == '__main__':
    main()

?

[–] grrgyle@slrpnk.net 49 points 1 week ago (8 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

load more comments (8 replies)
[–] HiddenLayer555@lemmy.ml 24 points 1 week ago* (last edited 1 week 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.

load more comments (5 replies)
load more comments (2 replies)
[–] eager_eagle@lemmy.world 33 points 1 week ago* (last edited 1 week ago) (3 children)

I work in an academic / research environment. Depending who wrote it, even seeing a __name__ == "__main__" is a bit of a rare thing...

[–] SpaceNoodle@lemmy.world 23 points 1 week ago (1 children)

Academic code is absolutely horrific.

Fortunately, it is possible to translate it for practical applications.

[–] arbitrary_sarcasm@lemmy.world 13 points 1 week ago

As someone in academia who writes code, I can confirm.

[–] HK65@sopuli.xyz 16 points 1 week ago (1 children)

Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope?

[–] eager_eagle@lemmy.world 15 points 1 week ago (1 children)

the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.

[–] HK65@sopuli.xyz 11 points 1 week ago

Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.

And don't forget the number one rule: you must use all the graphing libraries, all the time.

load more comments (1 replies)
[–] wise_pancake@lemmy.ca 14 points 1 week ago

Why would you waste a function call on something so completely redundant?

~For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.~

load more comments (3 replies)
[–] slacktoid@lemmy.ml 51 points 1 week ago (5 children)
load more comments (5 replies)
[–] embed_me@programming.dev 45 points 1 week ago (5 children)

Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language

[–] addie@feddit.uk 36 points 1 week ago (10 children)

I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.

To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.

Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.

load more comments (10 replies)
load more comments (4 replies)
[–] laurelraven@lemmy.zip 39 points 1 week ago (2 children)

One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren't whitespace

load more comments (2 replies)
[–] eager_eagle@lemmy.world 34 points 1 week ago (1 children)

The if block is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later.

[–] benni@lemmy.world 1 points 2 days ago

Interesting observation. Can you give an example where this is relevant?

[–] d_k_bo@feddit.org 33 points 1 week ago* (last edited 1 week ago) (1 children)

Still better than having to create a new class just to implement

public static void main(String[] args) {}

Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ

[–] Damarus@feddit.org 19 points 1 week ago (4 children)
[–] frezik@midwest.social 16 points 1 week ago

Only took 27 years to make the Java "Hello, world!" kinda sane.

load more comments (3 replies)
[–] _____@lemm.ee 26 points 1 week ago (3 children)

Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"

[–] drmoose@lemmy.world 20 points 1 week ago* (last edited 1 week ago) (8 children)

Tbh reserving "main" is just a hacky if not more so than checking __name__ if you actually understand language design.

load more comments (8 replies)
load more comments (2 replies)
[–] MTK@lemmy.world 22 points 1 week ago* (last edited 1 week ago) (10 children)

It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts

It is the excel of databases.

[–] frezik@midwest.social 13 points 1 week ago (16 children)

What's the difference between a "scripting" language and a "real" one?

load more comments (16 replies)
load more comments (9 replies)
[–] sheridan@lemmy.world 20 points 1 week ago (5 children)

Could someone explain this please? I'm still a noob.

[–] FooBarrington@lemmy.world 101 points 1 week ago (2 children)

Python has a bunch of magic variables, like __name__. This one contains the name of the module you're currently in (usually based on the file name), so if your file is called foo.py, it will have the value foo.

But that's only if your module is being imported by another module. If it's executed directly (e.g. python foo.py), it will instead have a __name__ of __main__. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.

[–] DragonTypeWyvern@midwest.social 73 points 1 week ago (1 children)

checks username

So it's you they're always talking about

load more comments (1 replies)
load more comments (1 replies)
[–] HiddenLayer555@lemmy.ml 39 points 1 week ago* (last edited 1 week ago) (2 children)

Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what's supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called main(). Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn't want to run an entire complex and resource intensive program if another program only needs to call a single function from it. In that case, the other program will call the function it wants but not main, so only that function executes and not the entire program.

However, Python is a scripting language that's interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn't have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn't need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn't natively support it, your main function won't do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven't called any functions.

The workaround is to have a single if statement at the root of the file that looks like this:

if __name__ == '__main__':
    main()

It checks a special variable called __name__. If the Python file is directly executed, __name__ will have the value of the string '__main__', which satisfies the if statement so main() is called. If another Python file imports it, the value of __name__ will be the name of that file, so main() isn't called. It's clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn't be writing it in Python.

[–] Scubus@sh.itjust.works 13 points 1 week ago

thats why i name my modules main.py

load more comments (1 replies)
load more comments (3 replies)
[–] Sinthesis 19 points 1 week ago* (last edited 1 week ago) (5 children)

I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params event and context. So I would do something like this:

def handler(event, context):
    things
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

if __name__ == '__main__':
    event = {}
    context = {}
    response = handler(event, context)
    print(response)
load more comments (5 replies)
[–] JATtho@lemmy.world 18 points 1 week ago (4 children)

I would put my code in a def main(), so that the local names don't escape into the module scope:

if __name__ == '__main__':
    def main():
        print('/s')
    main()

(I didn't see this one yet here.)

load more comments (4 replies)
[–] diemartin@sh.itjust.works 14 points 1 week ago* (last edited 1 week ago)
if debug.getinfo(1).what == "main" then
  -- ...
end

Not that you'll ever use it. No, seriously.

Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don't think there's an equivalent to Python's __name__ in stock Lua.

[–] match@pawb.social 12 points 1 week ago
[–] qx128@lemmy.world 11 points 1 week ago (3 children)

Nothing prevents you from putting a call to “main()” in the global scope

[–] jacksilver@lemmy.world 16 points 1 week ago (3 children)

The point of the name==main logic is that it checks if that is the file that was invoked (like running python filename.py). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).

load more comments (3 replies)
load more comments (2 replies)
[–] barsoap@lemm.ee 11 points 1 week ago
load more comments
view more: next ›