This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Samuel.Ainsworth, eryksun, steven.daprano
Date 2016-02-15.01:43:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1455500626.09.0.89892735427.issue26361@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-02-15 01:43:46eryksunsetrecipients: + eryksun, steven.daprano, Samuel.Ainsworth
2016-02-15 01:43:46eryksunsetmessageid: <1455500626.09.0.89892735427.issue26361@psf.upfronthosting.co.za>
2016-02-15 01:43:45eryksunlinkissue26361 messages
2016-02-15 01:43:44eryksuncreate