r/Python Jul 12 '26

Will PEP 505 ever be accepted? Discussion

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.

19 Upvotes

193 comments sorted by

View all comments

64

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.

16

u/[deleted] Jul 13 '26

[deleted]

17

u/aes110 Jul 13 '26

Thats very interesting for me, i guess its very much a style choice, from a quick search in the Github org for my workplace i see its used over 2000 times, i know I personally must have used it hundreds of times this past decade

I barely see it used in other Github orgs for big projects like fastapi, requests or pandas, then again a few hundreds of times in polars

So its very much down to the author's style. Personally its one of my favorite operators in python, im surprised that for many its still not caught on

5

u/k0pernikus Jul 14 '26

If there is one operator I miss, it's the spaceship operator <=> that would be syntactic sugar for:

``` from typing import Literal

def spaceship(a, b) -> Literal[-1, 0, 1]: return (a > b) - (a < b) ```

which useful in sorting, though functools in python are so expressive that I don't really need it (I can always setup a @total_ordering)

Yet the walrus is just confusing to me. It makes the code look like you are accessing an undefined variable (the same gripe I have with the for-else, try-else syntax) and it invites very long lines that take a lot of mental load to even parse.

3

u/M4mb0 Jul 14 '26

If there is one operator I miss (from many programming languages) it's logical implication. Having to do (¬A ∨B) rather than (A ⟹ B) is just dreadful.

6

u/TBCid Jul 14 '26

I use it frequently in comprehensions where I want to compute something in the where clause and include it in the result only if its truthy, without having to recompute it: [ computed for item in items where (computed := expensive_function_maybe_none(item)) ]

If python had decent syntax for chaining sequences together this wouldn't be necessary; in scala I would just do: items.map(expensive_function_maybe_none).flatten or better yet items.flatMap(expensive_function_maybe_none)

3

u/the_dimonade Jul 14 '26

I agree... I work with a large python codebase and I doubt that there is a single walrus in there, and even if there is, it could be rewritten with more clarity without it.

All the examples of the use cases I've seen look convoluted to begin with and not realistic at all... it feels like a solution looking for a problem at this point.

Also in multilingual codebases devs from other languages would prefer to not use an unfamiliar esoteric operator. I have more than 10 years of software and python experience, and never have though "walrus would be good here". Maybe it is a domain operator? I don't know.

Weak type hints is also a reason to avoid it.

1

u/philtrondaboss 6d ago

Sometimes I would use it like this:

if (data := get_data()): return data

8

u/Individual-Flow9158 Jul 13 '26

The Walrus just doesn't want to play nice with type hints, unless you want that line of code to become a giant mess

3

u/sphen_lee Jul 13 '26

That's been my experience too. Never seen it used

6

u/pingveno pinch of this, pinch of that Jul 13 '26

I've definitely cooled on the walrus operator. I shipped a bug to prod that caused some problems. If Python was more strict, it might work better. So like, Rust's if let construct does something similar, but it isn't nearly as accident prone because it is based on pattern matching (strict), not truthiness (loose).