PEP 505 – None-aware operators
peps.python.org
peps.python.org
As always, there is very good reasoning behind this PEP. And ultimately, when growing a language and becoming more "professional", complexity is added to the language or syntax.
From a personal point of view, I can get behind the following syntax change
value = my_object?.value
instead of doing if my_object:
value = my_object.value
else:
value = None
or even value = my_object.value if my_object else None
But looking into other examples given in the PEP, things can get quite messy and "ugly". mangle_from_ = policy?.mangle_from_ ?? True
I love Python and will continue to use it, both personally and professionally. I just hope the Python developers try to draw a line somewhere. Sometimes a design is good enough and further evolution / development should not be done, because every change can be justified in some way or another.> mangle_from_ = policy?.mangle_from_ ?? True
This scenario is the largest gain — nested nullable references is a massive pain, especially when you go deeper. It’s trivially superior to nested ternary or if statements.
IMO the benefits easily make it a worthwhile sugar to add — the only concern is whether it fits the python “aesthetic”; which to me is a flat no, but at the same time it seems like the walrus operator will be the defining moment of that aesthetic era coming to an end
But, I often solve this with a function like:
``` def default_to(x: Optional[T], d: T) -> T: return x if x is not None else d ```
I wonder if having this function as the official way to do it would solve the problem without the "specialness" of `??`.
But there's no good equivalent that I know of for `?.`, that's a real pain to do with python as it is now so it seems like a nice addition.