4 ms·
My immediate response to this is: WTF. Why not just do `l = len(x) if l is None else l`? With the new syntax can you define defaults computed from other defau
by monkeybutton 5y ago
My immediate response to this is: WTF.
Why not just do `l = len(x) if l is None else l`?
With the new syntax can you define defaults computed from other defaults? What about side effects? Why is computation happening in function args at all?!
- mrtranscendence 5y agoHaving the default value be None isn't great for documentation, and it isn't great for static typing, because now your argument has to be of type Union[Foo, None]. I've written functions where there are reasonably long chains of stuff like if foo is None: foo = [] and it'd be nice to be done with that.
- monkeybutton 5y agoI see what you're saying about typing. As for the reasonably long chains of default initialization code. Well now its in the definition. You could have functions with more code defining the parameters than in body.
- tinalumfoil 5y agoLWN had a good article on this: https://lwn.net/Articles/875441/ https://lwn.net/Articles/875441/ Short answer is these downsides were discussed but the proposal went ahead anyway. Imo I'm with you. This type of syntax has a lot of implicit behavior where it's going to be difficult to find/fix bugs related to it.
- monkeybutton 5y agoThat was a great article, thank you for the link!
- dragonwriter 5y ago> Why not just do `l = len(x) if l is None else l`? Because dev tooling can “see” and present information from the argument list much more easily than analyzing downstream behavior. > What about side effects? Those happen whether or not the computation is visible in the signature (though its more obvious to the programmer of the consuming code if it is visible.)