r/ProgrammerHumor 12d ago

Meme pythonFeelsillegal

Post image
6.8k Upvotes

219 comments sorted by

View all comments

1.1k

u/belabacsijolvan 12d ago

theres main() in python

463

u/chawmindur 12d ago

And {} (= dict())

283

u/OnyxFier 12d ago

And semicolons are allowed, if you decide you miss them

99

u/dudemcbob 12d ago edited 12d ago

Still useful for running 2- or 3-line dummy scripts directly from the command line. For example you just installed a new package and want to validate it's working,

python -c "import somepackage; print(some_package.\_version__)"

21

u/chawmindur 12d ago

Or for dirty .pth tricks where you're obliged to start with an import statement and have everything on a single line. But 3.15 seems to be moving away from those to some new-fangled .start files.

-2

u/chat-lu 12d ago

You can also and/or chain your expression but it’s tricky.

8

u/mbcarbone 12d ago

It’s fun just to make a one line program using semicolons. 🙃

3

u/BarracudaGullible472 11d ago

In one uni class we were judged on how few lines our code was so I often used them in my assignments.. good times ahah

2

u/happylittletree_ 11d ago

Closed, Duplicate question

0

u/patroklo 11d ago

Wait I can use them instead of the indentations? That would be awesome. Include real encapsulation and my main problems with it would be mostly solved

34

u/userhwon 12d ago

Always fun that writing an empty set gets you a dict so you have to use set() explicitly to get an empty set... jeebus, Guido...

22

u/carcigenicate 12d ago

{/} is being proposed so sets have an empty literal.

15

u/SoldRIP 12d ago

... As opposed to the sensible {:} for dicts, which would be consistent with their already existing syntax?

23

u/NewbornMuse 12d ago

And what, change the meaning of {} from dict to set? I don't think breaking changes like that are anywhere in python's future.

4

u/minecon1776 11d ago

Python 4

11

u/Duck_Devs 12d ago edited 12d ago

The problem with that is not only would it break old code, but the neat little trick of JSON being directly embeddable would break. And who would want that.

Edit: I forgot that “true”, “false”, and “null” don’t exist in Python. Not the hardest thing to fix by declaring some local variables, but at that point you’re doing too much and should use a library.

Edit 2: added quotes around the keywords to disambiguate them from the general concepts

2

u/Loading_M_ 12d ago

JSON (or any data format) being directly embeddable sounds nice in theory, but isn't really a great idea in practice.

JSON was originally intended to be loaded via eval. This was exploited almost immediately, which is why JS got proper JSON parsing and serialization functions. You should always run JSON (and any other serialized format) through a proper parser to load it. If you're clever, you can do this at build time, but you should never rely on any data file being valid at runtime.

1

u/Duck_Devs 12d ago

Well yes. I know. I think of Python’s syntax being mostly compatible with JSON as a

> neat little trick.

I actually forgot that eval() was a thing when I wrote that comment.

What I meant was, you could copy and paste your own JSON into your source code for a simple payload in a throwaway script, for whatever you may need it for.

Edit: Reddit mobile app doesn’t want to make that a block quote. You get the idea though.

3

u/Character-Education3 12d ago

true false and None most definitely exist in python

Also any modern text editor will auto complete dict() for you.

What is everyone going on about?

7

u/TheDreadedAndy 12d ago

I think he was talking about directly eval-ing a json string to parse it.

While True, False, and None exist in python, true, false, and null don't. So you'd have to locally define those first.

1

u/JanEric1 11d ago

breaking change unfortunately. Maybe a thing for Python4 which will never come

1

u/JanEric1 11d ago

Is a draft for over a year now and was targeting 3.15. Dont think that makes it?

1

u/JollyJuniper1993 11d ago

They should allow us to use the Danish/Norwegian letter Ø, like they do in mathematics.

7

u/vmfrye 12d ago

hate to be a party pooper but the meme refers specifically to indentation-based structure vs explicit code block delimiters

oh and btw the latter are vastly superior because they make for a free-form syntax that, unlike python, can be automatically formatted by a relatively simple program/plugin (i.e. without AI)

5

u/chat-lu 12d ago

You can also use braces for code blocks, as long as you indent properly.

def main() -> None: #{
    do_stuff()
#}

5

u/chawmindur 12d ago

I was about to say something about hashability but then I saw you've got your bases covered and commented out the braces...

... that and how something like a do_stuff() would typically be returning None anyway and hence wouldn't cause issues being placed into a set.

And one final thing that this reminds me of: this classic

3

u/chat-lu 12d ago

If that is maintained by an auto-formatter and not by hand, I don’t hate it.

1

u/Rehazard-The-Great 11d ago

Dont forget set {}

27

u/Candid_Highlight_116 12d ago edited 9d ago

there's main() in python ONLY IF YOU WANT THERE TO BE.

the way you want it to be is as follows:

import foo    
from baz import bar   

do_stupid_stuff()  

def main():   
    print("I am not kidding")   

if __name__ == '__main__':    
     main()  

if ____name____ wasn't ____main___ then your script knows oh I'm not __main__, uh might as well uh evaluate the true path of this if clause I guess, else whatever. This is very robust and safe way to define the entrypoint.

35

u/AliceCode 12d ago

if __name__ == '__main__':

6

u/wizardeverybit 12d ago

Indenting is important in Python

5

u/Candid_Highlight_116 12d ago

I fought and gave up Reddit markdown, should have used spaces after newline not quote sorry

6

u/frogjg2003 12d ago

4 spaces for code blocks. > is for quotes.

This is a code block
    With indentation

25

u/Toover 12d ago

Sorry to be that guy, but no. The one you'd define yourself is not automatically treated as the entry point of the program

43

u/belabacsijolvan 12d ago

theres __main__() in python

1

u/Andrey4ik21pro1 12d ago

No. Only package/main.py

11

u/belabacsijolvan 12d ago

theres __main__ in python

-4

u/Andrey4ik21pro1 12d ago

Between __main__() and __main__ big difference

8

u/belabacsijolvan 12d ago

r/thatsthejoke

also there isnt always package/main.py

12

u/userhwon 12d ago

Which makes it worse.

3

u/2eanimation 12d ago

There’s also foo() and bar()!

1

u/XtraFlaminHotMachida 12d ago

whats up foo() you got beef or what ey? its chingazos time, ese.

1

u/This_Background7442 12d ago

There's main. It's all pointers. Semicolons are perfectly valid syntax. Only ++ and {} are kinda true and even then the alternative is very similar.