01.03.21 23:59, Cameron Simpson пише: > On 28Feb2021 23:47, Alan Gauld <alan.ga...@yahoo.co.uk> wrote: >> On 28/02/2021 00:17, Cameron Simpson wrote: >>> BUT... It also has a __iter__ value, which like any Box iterates over >>> the subboxes. For MDAT that is implemented like this: >>> >>> def __iter__(self): >>> yield from () >> >> Sorry, a bit OT but I'm curious. I haven't seen >> this before: >> >> yield from () >> >> What is it doing? >> What do the () represent in this context? > > It's an empty tuple. The yield from iterates over the tuple, yielding > zero times. There are shorter ways to write that (eg outright omitting > the yield), except when you're writing a generator function with only a > single yield statement - then you need something like that to make it a > generator.
I was wondering what from following variants is more efficient: def gen1(): yield from () def gen2(): return yield def gen3(): return iter(()) $ python3.9 -m timeit -s 'def g(): yield from ()' 'list(g())' 1000000 loops, best of 5: 266 nsec per loop $ python3.9 -m timeit -s 'def g():' -s ' return' -s ' yield' 'list(g())' 1000000 loops, best of 5: 219 nsec per loop $ python3.9 -m timeit -s 'def g(): return iter(())' 'list(g())' 2000000 loops, best of 5: 192 nsec per loop -- https://mail.python.org/mailman/listinfo/python-list