6

Considering the code below:

class Test:
    l = [1, 2, 3]
    foo = lambda x: x

    for x in l:
        print(foo(x))

   [print(foo(x)) for x in l]


if __name__ == '__main__':
    test = Test()

The output is the following:

1
2
3
... in <listcomp>
    [print(foo(x)) for x in l]
NameError: name 'foo' is not defined

I don't understand why isn't foo a visible function from within the list iteration. Probably something to do with scopes but I am looking for the right explanation, with documentation to support it if possible.

ps: I am not interested in alternative implementations for the sake of just fixing the code.

renatodamas
  • 16,555
  • 8
  • 30
  • 51
  • no you can't, then ```x``` would essentially be ```self``` – renatodamas May 14 '21 at 11:24
  • 4
    https://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition – lllrnr101 May 14 '21 at 11:27
  • 1
    Does this answer your question? [Accessing class variables from a list comprehension in the class definition](https://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition) – dejanualex May 14 '21 at 11:30

2 Answers2

2

As suggested in the comments, this question seems to pretty much answer the issue and goes a long way to thoroughly explain it. There is however a small variation that can be solved following a similar approach. The resulting code is as follows:

class Test:
    l = [1, 2, 3]
    foo = lambda x: x

    (lambda foo=foo, l=l: [print(foo(x)) for x in l])()
renatodamas
  • 16,555
  • 8
  • 30
  • 51
0

You can also use map if your desired list comprehension is simple enough:

class Test:
    l = [1, 2, 3]
    foo = lambda x: x
    
    list(map(print, map(foo, l)))

Outputs

1
2
3
enzo
  • 9,861
  • 3
  • 15
  • 38