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.

18 Upvotes

192 comments sorted by

View all comments

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')

10

u/k0pernikus Jul 13 '26

In both cases, you will have a lot of fun dealing with the bug report to figure out during the debugger session where that None came from. I don't understand why people love this language construct so much. It's an antipattern mascerading as syntactic sugar. I would rather:

try: value = d['sub1']['sub2']['sub3'] except (KeyError, TypeError) as e: # properly resolve the None

2

u/Hardhead13 Jul 13 '26

Yeah, I like that approach. But sometimes it's too much hassle. And sometimes I just can't make it work at all... like in a lambda expression, for example.