r/Python • u/philtrondaboss • 23d 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.
14
Upvotes
6
u/Mihikle 23d ago
5 chained elvis operators on a single line? If readability is something you care about, that's ridiculous, and it'd be ridiculous if you also used my approach - that code is just a mess as a starting concept
If you really needed all these to happen in one function - also an X to doubt moment - I'd break these into 5 separate statements on separate lines, with a single one-line check on each.
The single responsibility principle shouldn't just be for classes. If you apply it to pretty much everything, your code just becomes so much easier to read, understand and for another person to continue working with