720

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 4 points 2 days ago* (last edited 2 days ago)

wait till you see

if __name__ = "__main__":

   main()
`
[-] HiddenLayer555@lemmy.ml 4 points 2 days ago* (last edited 2 days ago)

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 2 days ago* (last edited 2 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

[-] laurelraven@lemmy.zip 39 points 3 days ago

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)
[-] arschflugkoerper@feddit.org 86 points 4 days ago

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

[-] HiddenLayer555@lemmy.ml 74 points 4 days ago* (last edited 4 days ago)

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

[-] 30p87@feddit.org 63 points 4 days ago* (last edited 4 days ago)

Never heard of

def main():
    pass

if __name__ == '__main__':
    main()

?

[-] grrgyle@slrpnk.net 49 points 4 days ago

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 8 points 3 days ago

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

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

load more comments (7 replies)
[-] HiddenLayer555@lemmy.ml 23 points 4 days ago* (last edited 4 days 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 4 days ago* (last edited 4 days ago)

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 4 days ago

Academic code is absolutely horrific.

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

load more comments (1 replies)
load more comments (4 replies)
load more comments (4 replies)
[-] embed_me@programming.dev 44 points 3 days ago

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 3 days ago

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)
[-] d_k_bo@feddit.org 33 points 3 days ago* (last edited 3 days ago)

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 18 points 3 days ago
[-] frezik@midwest.social 16 points 3 days ago

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

load more comments (3 replies)
[-] JATtho@lemmy.world 18 points 3 days ago

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)
[-] slacktoid@lemmy.ml 51 points 4 days ago
load more comments (5 replies)
[-] MTK@lemmy.world 22 points 3 days ago* (last edited 3 days ago)

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 3 days ago

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

load more comments (16 replies)
load more comments (9 replies)
[-] _____@lemm.ee 26 points 3 days ago

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 3 days ago* (last edited 3 days ago)

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

Reserving main is definitely more hacky. Try compiling multiple objects with main defined into a single binary - it won't go well. This can make a lot of testing libraries rather convoluted, since some want to write their own main while others want you to write it because require all kinds of macros or whatever.

On the other hand, if __name__ == "__main__" very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.

load more comments (7 replies)
load more comments (2 replies)
[-] eager_eagle@lemmy.world 35 points 4 days ago

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.

[-] barsoap@lemm.ee 11 points 3 days ago
[-] Sinthesis@lemmy.today 19 points 3 days ago* (last edited 3 days ago)

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)
[-] dariusj18@lemmy.world 5 points 3 days ago

The if block is where my arg parser goes

[-] 10001110101@lemm.ee 3 points 2 days ago

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 2 days ago

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 2 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
  }
}
load more comments
view more: next ›
this post was submitted on 28 May 2025
720 points (96.3% liked)

Programmer Humor

23590 readers
1978 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