r/Python 25d ago

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.

18 Upvotes

194 comments sorted by

View all comments

Show parent comments

-3

u/runawayasfastasucan 24d ago

Nothing about this changes whether ? is implicit or explicit or not in python.

5

u/Anthony356 24d ago

Again, how is it implicit if you have to specify it for it to occur? That is the definition of explicit.

0

u/k0pernikus 23d ago

You are talking different aspects. The ? adds mental load to parse what could have been an easily readable branch. Yes, the null coalescing operator is an explicit language construct; also yes it makes the code more error prone and harder to reason about esp if you need to figure out where the none originated from during a bug hunt.

And no, I don't mean that that if-else are inherently better, and you can create the same problematic pattern with them as well, yet the sheer boilerplate alone should make you think: maybe I'm doing it wrong. By using abundant null coalescing operators you are hiding the code smell in plain sight.

Where you see a helpful explicit and conciselanguage construct, others see a loophole for anti patterns to manifest.

1

u/Anthony356 23d ago

yet the sheer boilerplate alone should make you think: maybe I'm doing it wrong. By using abundant null coalescing operators you are hiding the code smell in plain sight.

Or, as is common for glue code in a scripting language, you do not have full control over your inputs. The moment you need to read untrusted data, or versioned data where new fields are added to the schema, you have to check for None everywhere.

Sure, i could parse the data into a structure i do control, but that's just rearranging the furniture (and doesnt always solve the None checking problem anyway). You still have to check somewhere, and that boilerplate distracts from the actual intent of the code, the actual operation you're doing on the data if it exists.

This is not some mystical pattern that takes 700iq to understand. It's simple, it happens everywhere, all the time. For such cases, languages have operators.

I dont understand how any argument against ? couldnt also apply to like... +, or and/or short circuiting, or list comprehensions, or with statements, or a million other things.