The following code is transformed into a generator by the Python compiler:
def myGenerator(max):
for i in range(max):
yield i
It can be used with this code:
>>> gen = myGenerator(10)
>>> next(gen)
0
>>> next(gen)
1
If I query the class of gen, I get generator
.
Is it possible to use a custom generator class? Let's say I would like to have a percent property on gen
.
>>> gen.Percent
10
As far as I have understood generators, they implement several methods. Can I build a generator like object by myself and extend it by custom methods?