Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List comprehension
#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


Messages In This Thread
List comprehension - by Uchikago - Jul-17-2019, 02:46 AM
RE: List comprehension - by perfringo - Jul-17-2019, 07:46 AM
RE: List comprehension - by ThomasL - Jul-17-2019, 11:02 AM
RE: List comprehension - by scidam - Jul-18-2019, 12:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 546 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 494 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,264 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,426 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,739 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,867 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,256 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,233 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,705 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,157 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