How to recognize a generator function?

Bengt Richter bokr at oz.net
Wed Jun 18 22:46:50 CEST 2003


On Wed, 18 Jun 2003 13:20:53 -0700, Oscar Rambla <tecnic at codidoc.com> wrote:

>
>-Andrew. answered.
>>
>>How do you know what *any* function returns before you call it?
>
>I'll try to explain me better. Supose :
>
># a generator-function
>def f():
>  a =3D 1
>  yield a
>
># a kind of callback
>def call( object )
>  if isGeneratorfunction( object):
>      g =3D object()
>  elif Type(object) is GeneratorType:
>      object.next()
>
>call( f )
>
>The question is the following:
>
>def isGeneratorfunction():
>
>   return True

But why would anyone pass a generator object to call if call
was designed to take a no-parameter callable as its argument?

say gen =f() from above. Why not just pass gen.next instead of gen?
(or just write fgen = f().next and pass fgen instead of gen.next).
I.e., the bound method (note no parens on gen.next), which is callable
just like a function would be, and no test inside call would be necessary.

Regards,
Bengt Richter




More information about the Python-list mailing list