Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list comprehension vs for loop, within class definition

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.

like image 598
renatodamas Avatar asked May 14 '21 11:05

renatodamas


People also ask

What is the difference between list comprehension and for loop?

The for loop is a common way to iterate through a list. List comprehension, on the other hand, is a more efficient way to iterate through a list because it requires fewer lines of code. List comprehension requires less computation power than a for loop because it takes up less space and code.

Is list comprehension more efficient than for loop?

Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.

Why might you use a list comprehension instead of a loop in Python?

List comprehensions are faster than loops in general. It achieves to be faster by loading the entire list into the memory.

Are list comprehensions loops?

List comprehensions are also more declarative than loops, which means they're easier to read and understand. Loops require you to focus on how the list is created. You have to manually create an empty list, loop over the elements, and add each of them to the end of the list.

Is a for loop faster than a list comprehension?

As we can see, the for loop is slower than the list comprehension (9.9 seconds vs. 8.2 seconds). List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration. This is slow. Side note: It would even be worse if it was a Numpy Array and not a list.

What is list comprehension?

1 Comprehension of the list is an effective means of describing and constructing lists based on current lists. 2 Generally, list comprehension is more lightweight and simpler than standard list formation functions and loops. 3 We should not write long codes for list comprehensions in order to ensure user-friendly code. More items...

What are nested list comprehensions?

Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Below is the program which implements nested loop:

What is the difference between list comprehension and lambda expression in Python?

Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Now by using nested list comprehensions same output can be generated in fewer lines of code. Lambda Expressions are nothing but shorthand representation of Python functions.


1 Answers

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])()
like image 71
renatodamas Avatar answered Oct 21 '22 14:10

renatodamas