Cell-var-from-loop warning from Pylint
The name sort_key
in the body of the lambda
will be looked up when the function is actually called, so it will see the value sort_key
had most recently. Since you are calling sort
immediately, the value of sort_key
will not change before the resulting function object is used, so you can safely ignore the warning. To silence it, you can make sort_key
the default value of a parameter to the lambda
:
results.sort(key=lambda k, sk=sort_key: get_from_dot_path(k, sk), reverse=(order == -1))
Use functools.partial():
import functoolsresults.sort(key=functools.partial(get_from_dot_path, foo=sort_key), reverse=(order == -1))