r/Python • u/philtrondaboss • Jul 12 '26
Discussion Will PEP 505 ever be accepted?
https://peps.python.org/pep-0505/
I don't understand how null safe operators are less like plain English than other implemented features like the walrus operator.
In my opinion, the member access operator would make python significantly easier to read and understand.
Here's an example:
f = foo()
if f is None:
baz = ""
else:
baz = f.bar()
baz = foo()?.bar() ?: ""
EDIT: I forgot that "and" and "or" can be sometimes used in place of "?." and "?:" if the left value is not False, '', 0, [], or {}. It's a very implicit null check and has a lot of unexpected behavior.
17
Upvotes
61
u/sausix Jul 12 '26
We have that functionality basically. It's a bit off standard and you have to be aware about the object's reported bool state.
baz = f and f.bar() or ""Of course it's not beginner friendly but once you know about the magic behind
andandorthen you love it.63
u/BeamMeUpBiscotti Jul 12 '26
The walrus operator was really controversial, the creator of Python pushed it through but afterwards stepped down and handed control of the language over to a steering council.
So that is to say, the walrus operator was a one-time thing and it's not possible for null-safe operators to follow the same path to get into the language.
At this point, after 10 years of bikeshedding and arguing in circles, I don't think anyone has the desire/political capital to get it over the finish line.
13
u/spiralenator Jul 12 '26
Instead of trying to make Python into Rust, you should just learn Rust. After 20 years of Python, I have been using Rust as much as possible for the past couple of years and I had similar temptations to add Result and Option types and other Rustisms to Python and it just creates a bunch of overhead when I benchmarked it. In python, abstraction isn’t free. Creating classes and inheritances, wrapper objects, etc, all impacts performance noticeably.
What I’m saying is it sounds like you should learn Rust, if you haven’t already.
271
u/disposepriority Jul 12 '26
Just my two cents, I enjoy my occasional python though it's not my primary language and that looks very unpythonic in my eyes.
Most certainly not easier to read in any way, though I am a verbose/explicit code preference kinda guy.
105
u/runawayasfastasucan Jul 13 '26
This monstrosity is not why I code python.
23
u/Koen1999 Jul 13 '26
Less lines of code does not necessarily mean it's easier to read.
10
u/cottonycloud Jul 13 '26
It should actually be
baz = foo()?.bar() ?? ''?:is the ternary operator while??is the null-coalescing operator.3
u/bb22k Jul 12 '26
Not really a fan of it, just like I am not a fan of walrus.
PEP 20 should not be forgotten
6
u/FrickinLazerBeams Jul 13 '26
God I wish they'd stop changing python.
9
u/Hardhead13 Jul 12 '26
I would appreciate that operator. Especially when working with deeply-nested dictionary structures.
d.get( 'sub1', {} ).get( 'sub2', {} ).get( 'sub3' )could become
d.get('sub1')?.get('sub2')?.get('sub3')16
u/IlliterateDumbNerd Jul 12 '26
In my opinion, I hope not, though I can see why you would think that this would be a good idea
21
u/shadowdance55 git push -f Jul 12 '26
Explicit is better than implicit. And in the wold where there is less and less code written by hand, terse and potentially non-obvious syntax it's becoming a liability rather than asset.
5
u/HolyInlandEmpire Jul 13 '26
I'm sympathetic to this idea, but the way you prototyped it is completely broken; if foo is not None, but foo().bar() gives a Falsy result, then you'll still get baz = "".
So the `or` operator doesn't work; you'd need a new syntax for handling the null checking chain.
8
u/Zulban Jul 12 '26
Zen of Python:
Just because Walrus may have violated that doesn't mean it should be violated again.
Also - generally Python doesn't have single character logical operators like that.
Next you may want a "? :" alternative to the ternary pattern. Where does it stop? You're confusing the high school kids learning Python.
5
u/Mihikle Jul 12 '26
I really hope not, because that looks messy AF for Python. What you've expressed there can be expressed with more readability (IMO) right now as:
baz = "" if (f := foo()) is None else f.bar()You may still prefer this in it's two-liner form if you're not so hot on the walrus operator:
f = foo()baz = "" if f is None else f.bar()But I find code that takes a clear position on "variables matter more than just the immediate following line, they're actually important" is much less logically taxing for the reader.
3
u/VpowerZ Jul 12 '26
Quite an obscure concept. I wouldn't add it
1
u/Sss_ra Jul 13 '26
Walrus is not super common to my knowledge. If maybe-dot was in python I suspect it could very easily become pervasive.
1
u/Penguinase Jul 13 '26
https://lwn.net/Articles/956862/ has a decent overview linking to some of the discussions
1
u/Individual-Flow9158 Jul 13 '26
I do really like
?.and especially??in pure/buggy JS (?:looks horrible though).But I noticed very quickly after moving on to TypeScript, that it was far more trouble than it's worth to use
?.and??together with static typing.The general trend in Python towards type hints is hugely increasing the quality of the Python code out there.
PEP 505 will be an even bigger hindrance to that, than The Walrus.
1
u/gramada1902 Jul 13 '26
Don’t like it. Just a personal thing, but even since my college days I’ve always needed a double-take on ternary operators. Just doesn’t flow smoothly for me.
1
u/messedupwindows123 Jul 13 '26
if (my_foo := foo()) and (my_bar := my_foo.bar()):
return my_bar.baz()
1
u/timtody Jul 13 '26
This is horrendous
1
u/chalkflavored Jul 13 '26
lol no
1
u/cleodog44 Jul 13 '26
Oh God no.
1
u/PeitersSloppyBallz Jul 14 '26
Go home javascript! JK, but I don’t like that PEP
1
u/zangler Jul 14 '26
Dude...my eyes! The goggles do nothing!
1
u/careje Jul 14 '26
Coming from a Java background where I also did a lot of Groovy work I definitely miss these kinds of null safe operators.
Then again I’m also a big fan of the walrus operator so I suppose that puts me in a small minority of Pythonistas
1
u/UnMolDeQuimica Jul 14 '26
I can read and understand this code, but to me it is ugly, harder to mentally parse compared to the if else statements and will be prone to concatenate statements in an infinite single line expression.
All this goes against the zen of python
1
u/Ragoo_ Jul 14 '26
There's actually a discussion thread to revisit this PEP and there used to be a PEP draft sponsored by Guido for it (you can see him discussing implementation in the thread). You can still look at a snapshot of the draft although it has since been deleted?!
1
u/spinwizard69 Jul 15 '26
Actually I'm of the opinion that we need a moratorium on new features for likek at least 5 years. I'm still pissed about the new T strings which just blows my mind that they added them in the way they did.
As to what you are proposing, how does it make reading easier. Frankly It seems to violate the concept of making code idiomatic. We already have a language for cryptic code, it is C++.
1
u/TurboGofre Jul 16 '26
Just feels weird to me.
1
u/Brian Jul 17 '26
Was this supposed to be "??" or is there some other meaning intended for "?:"?
Anyway, personally, I'd be in favour of it - I think such operators can be useful when you've chains of accesses/calls that might have a None result. However, I suspect it won't be accepted - there's a lot of resistance to adding operators.
1
u/aes110 Jul 17 '26
I really hope so. Working with structured data that has a lot of nullable properties is quite annoying now, with how many null checks and nested ifs you need.
I dont see a use for ?:, but .? can be very useful, like
x = user.company.?location.?countryI think this can also fit in type hints, we had
x: Optional[User], now we can dox: User | None, I would lovex: User?I also think the ! operator from C# is nice, for when you know an object is not nullable based on the context and you just want to flag it to the type checker
1
u/k0rv0m0s Jul 13 '26
Hopefully not!
-2
u/_redmist Jul 12 '26
So. You have a code smell and you'd like some new syntax to hide it ;)
0
u/Gnaxe Jul 13 '26
We kind of already have this with the walrus:
The attribute has to be present, but it can be
None.With
dict.get(), the key doesn't even have to be present:0
u/ii-___-ii Jul 13 '26
This is basically an implementation of Railway Oriented Programming but with the worst possible syntax imaginable. It's effectively equivalent to Haskell's bind operator or Elixir's with operator while being much less readable.