[Python-Dev] feature request: inspect.isgenerator
Phillip J. Eby
pje at telecommunity.com
Thu Jun 1 18:07:09 CEST 2006
At 09:26 AM 6/1/2006 +0000, Michele Simionato wrote:
>Terry Reedy <tjreedy <at> udel.edu> writes:
> > To me, another obvious way is isinstance(object, gentype) where
> > gentype = type(i for i in []) # for instance
> > which should also be in types module.
>
>No, that check would match generator objects, not generators tout court.
>On a related notes, inspect.isfunction gives True on a generator, such
>as
>
>def g(): yield None
>
>This could confuse people, however I am inclined to leave things as they are.
>Any thoughts?
Yes, I think the whole concept of inspecting for this is broken. *Any*
function can return a generator-iterator. A generator function is just a
function that happens to always return one.
In other words, the confusion is in the idea of introspecting for this in
the first place, not that generator functions are of FunctionType. The
best way to avoid the confusion is to avoid thinking that you can
distinguish one type of function from another without explicit guidance
from the function's author.
I'm -0 on having an isgenfunc(), but -1 on changing isfunction. +1 on
making the code flags available. -1 on changing any other inspect stuff to
handle generators specially. They are not special and should not be
treated specially - they are just functions that happen to always return
generator-iterators -- and that is an *implementation detail* of the
function. Pushing that information out to introspection or doc is a bad
idea in the general case.
More information about the Python-Dev
mailing list