by basicwolf · 8y ago · 0 comments
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?No one has quite summed up my thoughts better than you.
Hell, I would love it and love to use it but I thing it's relatively out of place in Python. Python just has many more ways to solve what should be a rare discouraged situation.
I think it has a better place in languages like C#/Java that tend to have deeper, more rigid object hierarchies and more reasons for using nulls.