4 ms·
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
by basicwolf 8y ago
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?
- gt_ 8y agoSince Guido stepped down as BDFL (last month), I’m not sure there is yet much of a system in place for making these decisions. I would hope it isn’t left to absolute democracy. If this PEP is implemented, optimism will be difficult.
- pjz 8y ago...why does everyone forget that `getattr()` has an optional `default` argument? It's helpful for stuff like this.
- Doxin 8y agoSame reason people forget min and max have key arguments: You mostly don't need them until you really do.
- kjeetgill 8y ago> Does is look shorter? - YES!. Is it more readable? Arguably yes. Would I vote to see this feature in Python? HELL NO! 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.