Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Compiling Generators

Jukka Lehtosalo edited this pageOct 25, 2022 ·3 revisions

Generator functions must maintain state between invocations. A generator function is compiled into two classes. One is anenvironment class that maintains the local variables of the function, and the label/location where execution will continue(__mypyc_next_label__ attribute). The second class implements the actual generator. The environment class is similar to what mypyc uses to support nested functions.

For example, consider this simple generator:

deff(x:int)->Iterator[int]:print('a')yieldxprint('b')

This is compiled to something roughly like this (in heavily simplified pseudo-python):

deff(x:int)->Iterator[int]:_env=f_env()_env.x=x_env.__mypyc_next_label__=0_gen=f_gen()_gen.__mypyc_env__=_envreturn_genclassf_env:x:int__mypyc_next_label__:intclassf_gen:__mypyc_env__:f_envdef__next__(self)->int:_env=self.__mypyc_env___label=_env.__mypyc_next_label__if_label==0:print('a')_ret=_env.x_env.__mypyc_next_label__=1return_retelif_label==1:print('b')_env.__mypyc_next_label__=-1raiseStopIteration()else:raiseStopIteration()# Already completed

The actual generated code is more complicated because of error handling, andclose/throw/send methods.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp