def prefixes(s):
if s:
yield from prefixes(s[:-1])
yield s
t = prefixes('both')
next(t)
The next(t) returns 'b'. I'm just confused as to why this is because if we follow down the yield from statement, we will eventually end at yield from prefixes('') which would return None. In all my other tests yield from None raises a TypeError. Instead, this seems to just be ignored and prefixes('b') moves onto the next yield statement (? why does it do that?) to yield 'b'...
Any ideas as to why? Would greatly appreciate an explanation.
generators are lazy(on-demand) objects, you didn't exhaust your generator
t, to exhaust your generator you can use:now if you use
next(t)you will get the expectedStopIterationthe
ifstatement is "guaranteeing" that you have an end and you will never doNone[:-1]to get theTypeError