Message260290
For Python 3 you can also make it a keyword-only argument by adding a bare '*' to the parameter list:
funcs = [(lambda x, *, t=t: x * t) for t in range(5)]
Code that accidentally calls funcs[0](3, 'silent bug ') will raise a TypeError because "t" can only be passed as a keyword argument.
An alternative is to use another lambda instead of using a default argument:
funcs = [(lambda y: (lambda x: x * y))(t) for t in range(5)]
This lets you continue to use a closure (now over the temporary scope of the outer call) and keep the function signature free of extra arguments. However, using a keyword-only argument is obviously less obfuscated. |
|
Date |
User |
Action |
Args |
2016-02-15 01:43:46 | eryksun | set | recipients:
+ eryksun, steven.daprano, Samuel.Ainsworth |
2016-02-15 01:43:46 | eryksun | set | messageid: <1455500626.09.0.89892735427.issue26361@psf.upfronthosting.co.za> |
2016-02-15 01:43:45 | eryksun | link | issue26361 messages |
2016-02-15 01:43:44 | eryksun | create | |
|