726

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
[-] JATtho@lemmy.world 18 points 6 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.)

[-] YourShadowDani@lemm.ee 5 points 6 days ago

I'm a little new to Python standards. Is this better or worse than putting the def main(): outside the if statement (but calling main() inside it)

[-] JATtho@lemmy.world 5 points 6 days ago

I intended this an sarcastic example; I think it's worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the main() doesn't exist if you try import this as an module.

[-] myotheraccount@lemmy.world 6 points 6 days ago

I thought confusion about indent levels was the whole point of using python

[-] mexicancartel@lemmy.dbzer0.com 2 points 5 days ago

But it feels like main function should not be indented

[-] iAvicenna@lemmy.world 5 points 5 days ago* (last edited 5 days ago)

wait till you see

if __name__ = "__main__":

   main()
`
[-] HiddenLayer555@lemmy.ml 4 points 5 days ago* (last edited 5 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 5 days ago* (last edited 5 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

[-] dariusj18@lemmy.world 5 points 6 days ago

The if block is where my arg parser goes

[-] driving_crooner@lemmy.eco.br 6 points 6 days ago

Does everyone call the function of the script main? I never use main(), just call the function what the program is supposed to do, this program calculates the IBNR? The function is called calculate_IBNR(), then at the end of the script if name = 'main': calculate_IBNR(test_params) to test de script, then is imported into a tkinter script to be converter to an exe with pyinstaller

[-] Whelks_chance@lemmy.world 3 points 5 days ago

All of mine are called do_thing() because after a few days of working on it, the scope creep always means the original name was wrong anyway.

[-] 10001110101@lemm.ee 3 points 6 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 5 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 5 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
726 points (96.3% liked)

Programmer Humor

23627 readers
2180 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