3 ms·
One of the most fascinating uses of generators I've come across is a recursive generator >>> def count_binary(): ... yield "1" ... for pref
by animal_spirits 5y ago
One of the most fascinating uses of generators I've come across is a recursive generator
>>> def count_binary():
... yield "1"
... for prefix in count_binary():
... yield prefix + "0"
... yield prefix + "1"
...
>>> cb = count_binary()
>>> next(cb)
'1'
>>> next(cb)
'10'
>>> next(cb)
'11'
>>> next(cb)
'101'
[0] https://www.reddit.com/r/Python/comments/ovjubg/whats_the_most_simple_elegant_piece_of_python/h7ab7ki/ https://www.reddit.com/r/Python/comments/ovjubg/whats_the_mo...
- nestorD 5y agoThey are particularly nice when processing a recursive datastructure such as a tree. The technique can make for some very short and readeable code.
- kderbyma 5y agoI like them for building VMs quickly
- ianandrich 5y agoDo you have any example code?