113

For the following code:

for sort_key, order in query_data['sort']:
    results.sort(key=lambda k: get_from_dot_path(k, sort_key),
                 reverse=(order == -1))

Pylint reported an error:

Cell variable sort_key defined in loop (cell-var-from-loop)

Could anyone give a hint what is happening here? From pylint source code the description is:

A variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable.

But I do not have a clue what it means. Could anyone give an example of the problem?

Chris Martin
  • 29,484
  • 8
  • 71
  • 131
xis
  • 23,504
  • 9
  • 41
  • 58

2 Answers2

127

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))
drew
  • 403
  • 5
  • 11
chepner
  • 446,329
  • 63
  • 468
  • 610
  • 11
    I'd err on the side of fixing the issue instead of ignoring the warning. If possible, I'd use `key=partial(get_from_dot_path, foo=sort_key)` instead of the lambda expression (assuming there is some parameter name `foo` defined by `get_from_dot_path` that you can use for a keyword argument; `partial` only allows filling in positional parameters exclusively from the left). – chepner Dec 13 '18 at 15:17
  • 1
    Ah I didn't realise this would fix it, I thought they were equivalent; in that case I agree. – Tim Diels Dec 14 '18 at 14:42
  • 3
    be aware that currently the trick does not always work https://github.com/PyCQA/pylint/issues/3107 – Daniel Pinyol Sep 16 '19 at 13:39
  • 1
    Using [`nonlocal`](https://docs.python.org/3/reference/simple_stmts.html#nonlocal) did _not_ help, I had to use [partial()](https://docs.python.org/3/library/functools.html#functools.partial) as suggested by @chepner. – Jens Feb 16 '21 at 10:49
7

Use functools.partial():

import functools
results.sort(key=functools.partial(get_from_dot_path, foo=sort_key),
             reverse=(order == -1))
mejdev
  • 3,199
  • 4
  • 23
  • 36
  • can confirm this not only gets rid of the warning, but really fixes the problem. Try for example making a list of lambdas print the range 1,10 - with lambdas, they all print 9, with functools.partial they print each value – TamaMcGlinn Jun 21 '21 at 12:11