5 ms·
PEP 505 – None-aware operators
- michaelanckaert 5y agoLanguage syntax is always a highly personal matter, subject a coder's preferences. That being said, I'm not very fond of the latest changes that have been implemented in the Python language. The "walrus operator" being the latest example. 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.
- setr 5y agoThese have existing exactly the same in C# for a bit, and they’re fantastic bits of sugar — null checking comes up everywhere so its a low-impact high-volume benefit; looking at it as a one-off is pointless (see fstrings as a similar change) > 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
- davidshepherd7 5y agoAnother problem with the current situation which isn't mentioned in the PEP: I often see people writing `foo_bar or 1` instead of `foo_bar if foo_bar is not None else 1`, which is usually incorrect when a is a number (zero is falsey). I think a terser "official" way of doing this would help there. 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.
- kelseyfrog 5y agoJust provide do-notation and be done with it. Operators for specific instantiations of a much broader abstract concept always irks me. This path leads to the hell of special purpose operators for a closed set of concrete use-cases.