Not coalescing around None-aware
Not coalescing around None-aware
Posted Dec 23, 2022 14:46 UTC (Fri) by shironeko (subscriber, #159952)In reply to: Not coalescing around None-aware by rrolls
Parent article: Not coalescing around None-aware
Posted Dec 23, 2022 17:44 UTC (Fri)
by rrolls (subscriber, #151126)
[Link] (5 responses)
There are plenty of examples in the PEP that demonstrate where ?? would be useful far better than I could.
Posted Dec 23, 2022 19:30 UTC (Fri)
by shironeko (subscriber, #159952)
[Link] (4 responses)
One common example in there is the practice of foo(optional=None) and then assigning a default value to optional right afterwards. clearly this calls for improving the default value syntax not the none operator because it'll still be ugly, just shorter. Maybe the default syntax should accept function call, then we get the added benefit of having the function signature more self documenting than just None and have to figure out what that means.
Posted Dec 24, 2022 6:35 UTC (Sat)
by NYKevin (subscriber, #129325)
[Link] (3 responses)
Improving the default syntax has been discussed endlessly, but it's probably never going to happen, because both of the obvious approaches are non-starters:
1. You can't introduce a new default syntax, because then you would have two default syntaxes, and that has generally been regarded as inferior to the status quo.
Posted Dec 24, 2022 6:42 UTC (Sat)
by shironeko (subscriber, #159952)
[Link] (2 responses)
Posted Dec 24, 2022 9:12 UTC (Sat)
by NYKevin (subscriber, #129325)
[Link]
My interpretation of your comment is that you want to allow us to write an arbitrary expression as the default value, and lazily evaluate that expression when the function is called. It is already legal Python syntax to put arbitrary expressions as the default value, so this example will demonstrate the backwards incompatibility of the second half (lazy evaluation) only:
If you make x=i evaluate lazily, then both versions would print 4. This is backwards incompatible, and it also leaves us with no obvious way to get the 2 behavior instead of the 4 behavior. (Of course, it can be done, but it requires additional boilerplate that nobody should have to write.)
On the other hand, if you are proposing some new syntax that doesn't look like a "regular" Python expression (or somehow "decorates" the default value to indicate that it should be evaluated lazily)... nobody wants to deal with two different default value syntaxes in the same language. That way lies madness.
Posted Dec 24, 2022 9:14 UTC (Sat)
by smurf (subscriber, #17840)
[Link]
Personally I'd just re-purpose the Walrus operator to do lazy evaluation when you (ab)use it in a "def" declaration, though that's probably a non-starter given the past contentious discussion on that topic.
Not coalescing around None-aware
Not coalescing around None-aware
Not coalescing around None-aware
2. You can't modify the semantics of the existing syntax, both because it would break backwards compatibility, and also because there is an actual use case for eager evaluation of default values (namely, forcing a closure to eagerly evaluate its closed-over variables) and so you would need to provide an alternative for that. The total amount of ugliness in the language would probably increase.
Not coalescing around None-aware
Not coalescing around None-aware
lazy = []
eager = []
for i in range(5):
lazy.append(lambda: i)
eager.append(lambda x=i: x)
print(lazy[2]()) # Prints 4
print(eager[2]()) # Prints 2
Not coalescing around None-aware