[Python-ideas] Generators are iterators
Ron Adam
ron3200 at gmail.com
Sun Dec 14 02:33:14 CET 2014
On 12/13/2014 03:27 PM, Chris Angelico wrote:
> On Sun, Dec 14, 2014 at 7:19 AM, Ron Adam<ron3200 at gmail.com> wrote:
>> >I think if function definitons with yield in them created
>> >generator_instance_factory objects instead of function objects, things would
>> >be easier to explain. What it comes down to, is they aren't normal
>> >functions and it's a bit confusing to think of them that way.
> They're no more different than, say, a decorated function that returns
> a different function.
But that is different, because the decorator isn't visible.
You can also offer examples of other objects in terms of other objects,
they are still other objects.
So it raises questions like this ... When should we make a distinction of
one object from another? And when shouldn't we?
Usually it has more to do with weather or not it makes writing some program
easier. I'm not sure changing the type in this case would be useful in
that way.
> But if you want a different way to explain them,
> here's a similar way to craft an iterator:
I think it's what the average python user expects that matters.
The term generator_function was use quite a lot in the PEP discussion in
order to distinguish it from a regular function, and also from the
generator returned from it. So it seems to me, that maybe there is
something to that.
Currently (especially when discussing things here), I think it's important
to try to have an accurate model of what is being discussed. Comparisons
to similar code is also useful, but for checking the implementation works
correctly and consistently. They don't replace the accurate model. That
model could be pseudo-code, or python, or a diagram, but it helps to keep
in in mind.
So, we have a rule we need to remember:
If a function has "yield" in it, it will return a generator-object.
Ok. So a generator is: a function + that rule.
We have a case where we differentiate it in our minds, but the code,
type(g) indicates it is not different, but the code actually does do
something a bit magical. Yes we can visually examine the body of the
function and tell that it's different, but that isn't full proof.
That is some of what leads me to think it should maybe be a different type.
Possibly a sub-class of a function class, would be the only option.
Fortunately generators are not new, so most everyone does understands how
to define and use them, just not how the implementation is actually done.
But maybe that's ok. <shrug>
Cheers,
Ron
More information about the Python-ideas
mailing list