Last active
May 12, 2025 21:57
-
-
Save makslevental/9b435b04f267becd2de54b830aed021c to your computer and use it in GitHub Desktop.
gen conundrum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dis | |
import inspect | |
def my_generator(): | |
frame = inspect.currentframe() | |
print(frame.f_code.co_consts) | |
x = yield | |
frame = inspect.currentframe() | |
print(frame.f_code.co_consts) | |
print("Received:", x) | |
y = yield x * 2 | |
print("Received:", y) | |
gen = my_generator() | |
next(gen) # Start the generator | |
gen.send(10) # Send 10 to the first yield | |
print(dis.dis(my_generator)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(None, 'Received:', 2) | |
(None, 'Received:', 2) | |
Received: 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 0 RETURN_GENERATOR | |
2 POP_TOP | |
4 RESUME 0 | |
6 6 LOAD_GLOBAL 1 (NULL + inspect) | |
16 LOAD_ATTR 2 (currentframe) | |
36 CALL 0 | |
44 STORE_FAST 0 (frame) | |
7 46 LOAD_GLOBAL 5 (NULL + print) | |
56 LOAD_FAST 0 (frame) | |
58 LOAD_ATTR 6 (f_code) | |
78 LOAD_ATTR 8 (co_consts) | |
98 CALL 1 | |
106 POP_TOP | |
8 108 LOAD_CONST 0 (None) | |
110 YIELD_VALUE 1 | |
112 RESUME 1 | |
114 STORE_FAST 1 (x) | |
9 116 LOAD_GLOBAL 1 (NULL + inspect) | |
126 LOAD_ATTR 2 (currentframe) | |
146 CALL 0 | |
154 STORE_FAST 0 (frame) | |
10 156 LOAD_GLOBAL 5 (NULL + print) | |
166 LOAD_FAST 0 (frame) | |
168 LOAD_ATTR 6 (f_code) | |
188 LOAD_ATTR 8 (co_consts) | |
208 CALL 1 | |
216 POP_TOP | |
11 218 LOAD_GLOBAL 5 (NULL + print) | |
228 LOAD_CONST 1 ('Received:') | |
230 LOAD_FAST 1 (x) | |
232 CALL 2 | |
240 POP_TOP | |
12 242 LOAD_FAST 1 (x) | |
244 LOAD_CONST 2 (2) | |
246 BINARY_OP 5 (*) | |
250 YIELD_VALUE 1 | |
252 RESUME 1 | |
254 STORE_FAST 2 (y) | |
13 256 LOAD_GLOBAL 5 (NULL + print) | |
266 LOAD_CONST 1 ('Received:') | |
268 LOAD_FAST 2 (y) | |
270 CALL 2 | |
278 POP_TOP | |
280 RETURN_CONST 0 (None) | |
>> 282 CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) | |
284 RERAISE 1 | |
ExceptionTable: | |
4 to 280 -> 282 [0] lasti | |
None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment