How to identify generator/iterator objects?

K

Kenneth McDonald

I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.

To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:
.... for s in x: yield s
....<type 'function'>

So while I can identify iterators, I can't identify generators by class.

Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.

Thanks,
Ken
 
P

Paddy

Kenneth said:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.

To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:

... for s in x: yield s
...
<type 'function'>

So while I can identify iterators, I can't identify generators by class.

Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.

Thanks,
Ken
.... for s in x: yield s
....
<generator object at 0x01388E18>

?
- Paddy.
 
L

Leo Kislov

Kenneth said:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.

To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:

... for s in x: yield s
...
<type 'function'>

So while I can identify iterators, I can't identify generators by class.

But f is not a generator, it's a function returning generator:
.... print "Hello"
.... yield 1
....Traceback (most recent call last):

Notice, there is no side effect of calling f function.

-- Leo
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Kenneth said:
To do this, I need to determine (as fair as I can see), what are
Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.

As others have pointed out, there is a proper test for generator
objects; you are apparently interested in finding out whether a
function will produce a generator when called.

To do that, use the following code

def is_generator_function(f):
return (f.func_code.co_flags & 0x20) != 0

Here, 0x20 is the numeric value of CO_GENERATOR (also available
through compiler.consts.CO_GENERATOR).

Regards,
Martin
 
P

Paddy

Kenneth said:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.

To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:

... for s in x: yield s
...
<type 'function'>

So while I can identify iterators, I can't identify generators by class.

Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.

Thanks,
Ken

Unfortunately, nothing is as easy as it may seem:
.... return f.func_code.co_flags & CO_GENERATOR != 0
........ for s in x: yield s
........ def g(y):
.... for s in y: yield s
.... return g(x)
....

;-)

- Paddy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top