简体   繁体   中英

Python: Create Generators at Runtime

I know in python classes and functions can be created at runtime using type and lambda respectively, but can generators be created at runtime?

Example: keyword, condition, action, yield

lambda x,a: a<x,a++,a

Is this what you are asking for? If by defined at runtime, you mean, without having to explicitly create a function that uses the yield keyword, then yes, you can use generator expressions .

>>> t = [1, 2, 3]
>>> (i*2 for i in t)
<generator object <genexpr> at 0x7f1f72dbfdc0>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM