We have a lot of Django-based code similar to
if getattr(obj, 'foreign_obj') is not None and getattr(obj.foreign_obj, 'field_name') is not None:
Would this turn into: if obj?.foreign_obj?.field_name
?Does is look shorter? - YES!. Is it more readable? Arguably yes. Would I vote to see this feature in Python? HELL NO! In most of cases, we can have an in-house "maybe()" function, like
maybe(obj, 'foreign_obj.field_name')
There is no need to update the language syntax for that.From PEP description:
From bisect.py:
def insort_right(a, x, lo=0, hi=None):
# ...
if hi is None:
hi = len(a)
# ...
After updating to use the ??= augmented assignment statement:
def insort_right(a, x, lo=0, hi=None):
# ...
hi ??= len(a)
# ...
Seriously? To me the "if hi is None" looks times more readable and easily comprehensible than "hi ??= len(a)".Finally, The Zen of Python
Special cases aren't special enough to break the rules.
Although practicality beats purity.
This case does not look neither special enough nor so much practical to me. Could anyone please give a hint of where can one vote against this PEP?