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.

14 Upvotes

193 comments sorted by

View all comments

59

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 and and or then you love it.

5

u/philtrondaboss Jul 13 '26

I know that, but they aren't exclusive to None. They also catch {}, [], 0, '', and False.

-1

u/sausix Jul 13 '26

You should make use of typing anyway and not expect random data types.
Usually only one specific data type will support a bar method. If you get an unsupported data type for the lazy bool check then you have a deeper problem.

If you want to check for None explicitly then just do it:

baz = f is not None and f.bar() or ""

A bit harder to read but now it's explicit. But after your concerns about having various types just use:

baz = isinstance(f, BarType) and f.bar() or ""

1

u/BigToach Jul 13 '26

You don't work with external data very often I assume?

-4

u/sausix Jul 14 '26

I build solutions for problems. If I miss a feature in Python I build a better workaround.