How to recognize a generator function?

Bernhard Herzog bh at intevation.de
Wed Jun 18 16:16:23 CEST 2003


Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:

> On Wed, 18 Jun 2003 03:21:35 -0700, Oscar Rambla <tecnic at codidoc.com>
> wrote:
[...]
> >How to recognize if a function is a generator function before calling it?

Why do you need to recognize that?

> Let us see...
> 
> >>> from __future__ import generators
> >>> def testfunc():
> ... 	return None
> ... 
> >>> testfunc
> <function testfunc at 0x010F05C0>

>>> testfunc.func_code.co_flags
4099

> >>> def testfunc():
> ... 	yield None
> ... 	
> >>> testfunc
> <function testfunc at 0x010F2750>
> >>> 

>>> testfunc.func_code.co_flags
4131

So it's possible to distinguish the two, but I don't see why anyone
would care (except in some very special circumstances such as a
debugger, maybe).

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list