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

Programmer Humor

23784 readers
2569 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

[–] 10001110101@lemm.ee 3 points 6 days ago (1 children)

I've always found needing to manually add a class instance parameter (i.e. self) to every object method really weird. And the constructors being named __init__. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it's the language I use the most, lol).

[–] sebsch@discuss.tchncs.de 3 points 6 days ago (1 children)

Nah self is quite important. The main part of a method is to access the state of the object. self is just the interface to it.

[–] 10001110101@lemm.ee 4 points 6 days ago

Guess I just prefer languages that do it this way:

class AClass {
  var aProp = 0

  fun aMethod() {
    aProp++
  }
}

Though I suppose confusion and bugs can happen when you do something like:

class AClass {
  var aProp = 0

  fun aMethod(aProp: Int) {
    // `this.aProp` is needed to access the property
  }
}

Depends on how lazy I am at the moment.

[–] JoYo@lemmy.ml 0 points 6 days ago

main.py or did you not read the manual?

[–] 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.

load more comments (1 replies)
[–] HK65@sopuli.xyz 16 points 1 week ago (2 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?

load more comments (2 replies)
load more comments (1 replies)
load more comments (4 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 (9 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.

[–] ebc@lemmy.ca 2 points 6 days ago (1 children)

other people's Java

I'm gonna have to disagree here, it's always a guessing game of how many layers of abstraction they've used to seemingly avoid writing any implementation code... Can't put the code related to "bicycles" in the Bicycle class, no, that obviously goes in WheeledDeviceServiceFactoryBeanImpl that's in the 'utils' package.

[–] addie@feddit.uk 2 points 6 days ago

Enough of that crazy talk - plainly WheeledDeviceServiceFactoryBeanImpl is where the dependency injection annotations are placed. If you can decide what the code does without stepping through it with a debugger, and any backtrace doesn't have at least two hundred lines of Spring boot, then plainly it isn't enterprise enough.

Fair enough, though. You can write stupid overly-abstract shit in any language, but Java does encourage it.

load more comments (8 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 1 day 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)
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) (7 children)

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

load more comments (6 replies)
load more comments (2 replies)
[–] MTK@lemmy.world 22 points 1 week ago* (last edited 1 week ago) (26 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.

load more comments (26 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.

load more comments (2 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)
load more comments
view more: next ›