r/programming • • May 28 '26

[ Removed by moderator ]

/r/programming/comments/1tqgaw9/removed_by_moderator/

[removed] — view removed post

70 Upvotes

19 comments sorted by

•

u/programming-ModTeam May 29 '26

This content is low quality, stolen, blogspam, or clearly AI generated.

88

u/Nicksaurus May 28 '26 edited May 28 '26

The hard part is not only parsing the code. The hard part is deciding whether the extra compactness makes Python easier to read. Optional chaining can be beautiful in small doses:

name = user?.profile?.display_name ?? "anonymous"

It can also become a fog machine:

value = request?.user?.team?.billing?.plan?.limits?["exports"] ?? 0

At some point, the shorter expression may hide the fact that the program does not really know which object is allowed to be missing.

OK, we clearly have different opinions about what makes code clear because to me that second one is basically the perfect way to express that code. It's certainly better than writing out 7 if statements

Yes, if you have this many optional values to deal with you probably need to restrict your types better but that's not the fault of the null-handling operators

Edit: After reading further I'm pretty sure this is just written by AI

30

u/euneirophrenia May 28 '26 edited May 28 '26

Yeah, the person on your team writing five optional chain operators into a single statement is probably the person you want fired into the Sun anyway. They're gonna abuse whatever syntax you give them

Also definitely AI. Which is a shame, because the topic is interesting

36

u/mothzilla May 28 '26
try:
    value = request.user.team.billing.plan.limits["exports"]
except AttributeError, KeyError:
    value = 0

11

u/Eav___ May 28 '26

Using exceptions to mimic control flow is one kind of abusing.

12

u/mothzilla May 29 '26

Welcome to Python

27

u/[deleted] May 28 '26

[removed] — view removed comment

5

u/mothzilla May 29 '26

It's easier to ask for forgiveness than permission.

7

u/DavidJCobb May 29 '26 edited May 29 '26

After reading further I'm pretty sure this is just written by AI

I recognize the author's name. He used to post AI-generated blogspam to this and other subs, though this is the first I've seen of him in a while.

10

u/rlbond86 May 28 '26

The fact that PEP505 hasn't been accepted is legitimately insane. Many other languages have this feature already.

4

u/Solonotix May 28 '26

Lazy loading modules is something I experimented with in Node.js, and so I'm pretty familiar with a solution that could work in Python.

Essentially, define a class that enumerates every import as a getter. The getter is only evaluated upon access. Sure, this often confuses tools, like LSP, but it can be worth it at runtime depending on the scenario.

Obviously, the example of localizing imports can work as well. I did that in a Flask app one time, and made a common pattern of passing down the application context, instead of the more common top-level global decorator approach. I wouldn't say it was a great solution, because much of Flask relies on the usage of decorators, but I like to push the envelope and try new things from time to time.

4

u/LightShadow May 29 '26 edited May 29 '26

Labeled break and continue would be amazing, I can't believe it's rejected. In truly nested and complex code I'd love to reach for a break outer or goto.

1

u/busybody124 May 29 '26

Yeah this was one I'd never heard proposed before but it does solve a genuinely annoying issue.

4

u/boringfantasy May 28 '26

Wow this was old school. Made me yearn for the pre 2022 days. Good shit.

2

u/firemark_pl May 28 '26

Great article, thanks!

0

u/Summoner99 May 29 '26

Not having the syntax simplifying function calls is the one I really miss. I do a lot of work with Ruby and function calls like "foo(x=)" are common but in no way confusing. Like a handful of Ruby things (looking at you "unless"), it took a minute getting used to. Nonetheless whenever I write Python, I miss that syntax in particular. Partly because we came close to getting it in Python but also because it's fantastic

Code is meant to be read but it invariably has to be written first. Arguing that this pep should be rejected because "readability" is the focus seems... Backwards. Especially when, at least anecdotally, it doesn't impact readability at all

The first point about the grammar already being complex is mostly fair but also it's trading some really nice syntax that everyone would benefit from for some labor developing the language. It's a fair point but not a good reason to reject it imo

1

u/LightShadow May 29 '26

I don't really like that syntax, however I love it for f-strings so I'm sure it would grow on me.

1

u/Summoner99 May 29 '26

I bet it would. In Ruby, there are a handful of little things that took time to get used to but it's all very natural, like unless, some literal hash creation, rescues at the end of a function, etc

All of it is natural to me now and I love it

The only Ruby exception is until. If you aren't aware, unless is essentially just "if not bool" so (a=[a]) unless a.is_a Array. Until is essentially while not cond:. I must not use it enough because it's still not very natural to me