9 ms·
Not sure I'm getting why we need a => for a default function argument instead of just reusing the = symbol...
by harel 5y ago
Not sure I'm getting why we need a => for a default function argument instead of just reusing the = symbol...
- sushibowl 5y agoBecause that would make the change backwards incompatible. Some code out there could be (ab)using the fact that default arguments are bound at function definition time, and depend on that behavior to stay as it is. So this new behavior must be behind a new syntax.
- harel 5y agoBut said old code is using an older version of Python, which does not support this. The compiler could issue a warning for newer version until the switchover, and therefore save us from this "special case".
- new_user_final 5y agoUnfortunately, the author used some unnecessary words. >Function parameters can have default values which are calculated during function definition and saved. This proposal introduces a new form of argument default, defined by an expression to be evaluated at function call time
- harel 5y agoThat's much clearer. Thanks
- eloisius 5y agoSo it’s not specific to functions as arguments? A long-time gripe I’ve had about default parameters in Python is that they are static from function definition time instead of dynamic at call time. A consequence of that is that if you have a function like def foo(bar, baz=[]): baz.append(bar) return baz baz will be reused for the duration of the process kind of like a global variable. If this new syntax fixes that, I see the value.
- Sohcahtoa82 5y ago> If this new syntax fixes that, I see the value. It does. The old syntax creates and assigns the variable at compile-time. => does it at execution time. So this code: def foo(bar, baz=[]): baz.append(bar) return baz foo(1) foo(2) print(foo(3)) would print '[1, 2, 3]' But this code: def foo(bar, baz=>[]): baz.append(bar) return baz foo(1) foo(2) print(foo(3)) would print '[3]'
- deleted 5y ago[deleted]