Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List comprehension
#1
Please explain why this doesn't work
seq=[1,2,3]
def a(): return x+2
x=[a() for x in seq]
I thought a list comprehension create a new local scope for the expression itself, and when we use a(), x is searched by the LEGB rule and can find the x in the enclosing scope, am i correct ?
Reply
#2
Following is friendly banter. No offence intended.

These repeated questions reminded me 'sketch' from Three Men in a Boat by Jerome K Jerome.

One can read whole episode here, but for immediate usage I provide prescription:

Quote:1 lb. beefsteak, with
1 pt. bitter beer
every 6 hours.
1 ten-mile walk every morning.
1 bed at 11 sharp every night.
And don’t stuff up your head with things you don’t understand.

As for question: it's always good practice to use functions with parameters.

>>> seq = [1, 2, 3]                                                        
>>> def a(x): return x + 2                                                 
>>> lst = [a(num) for num in seq]                                          
>>> lst                                                                    
[3, 4, 5]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Jul-17-2019, 02:46 AM)Uchikago Wrote: Please explain why this doesn't work
seq=[1,2,3]
def a(): return x+2
x=[a() for x in seq]
Sorry but this code is complete nonsense and that´s the reason it´s not working.
You definitely should watch some tutorial, e.g. this one here.
Reply
#4
Not sure, is this question very simple, or very tricky...So, lets do some examples here.
First of all, lets start with the following example:

def a():
    return x + 2

for x in range(10):
    print(a(), globals().get('x'))
Output:
2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9
Everthing works as expected: for loop creates x-values in the global scope, and the variable x is visible from the a()-function.

List comprehensions are functions too. To show this, lets consider the following bytecode, e.g.
import dis
dis.dis("[x for x in range(5, dont_execute_me='I am in op-code')]")
Output:
1 0 LOAD_CONST 0 (<code object <listcomp> at 0x7f8a0d898ed0, file "<dis>", line 1>) 3 LOAD_CONST 1 ('<listcomp>') 6 MAKE_FUNCTION 0 9 LOAD_NAME 0 (range) 12 LOAD_CONST 2 (5) 15 LOAD_CONST 3 ('dont_execute_me') 18 LOAD_CONST 4 ('I am in op-code') 21 CALL_FUNCTION 257 (1 positional, 1 keyword pair) 24 GET_ITER 25 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 28 RETURN_VALUE
Line #25 executes list comprehension-function; line #21 creates range;

How to make it works...

We just need to declare something like this: [a() for x in range(5); global x]; but this isn't allowed by Python syntax.

Nevertheless, we can do some injection, e.g.

[(globals().update({"x":x}),  a())[-1] for x in range(3)]
Output:
[2, 3, 4]
Everything works fine! This is just for fun, don't do this in any valuable code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,680 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,201 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,177 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,413 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,073 Oct-10-2020, 05:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020