Eryk Sun added the comment:

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.

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26361>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to