473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to identify generator/iterator objects?

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:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
.... for s in x: yield s
....
>>f
<function f at 0x58230>
>>f.__class__
<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
Oct 25 '06 #1
4 1570

Kenneth McDonald wrote:
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:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<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
>>def f(x):
.... for s in x: yield s
....
>>f([1])
<generator object at 0x01388E18>

?
- Paddy.

Oct 25 '06 #2

Kenneth McDonald wrote:
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:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<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:
>>def f():
.... print "Hello"
.... yield 1
....
>>iter(f)
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: iteration over non-sequence
>>iter(f())
<generator object at 0x016C7238>
>>type(f())
<type 'generator'>
>>>
Notice, there is no side effect of calling f function.

-- Leo

Oct 25 '06 #3
Kenneth McDonald schrieb:
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
Oct 25 '06 #4

Kenneth McDonald wrote:
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:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<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:
>>def is_generator(f):
.... return f.func_code.co_flags & CO_GENERATOR != 0
....
>>def f(x):
.... for s in x: yield s
....
>>is_generator(f)
True
>># But look at the following:
def f2(x):
.... def g(y):
.... for s in y: yield s
.... return g(x)
....
>>f2([1,2,3])
<generator object at 0x013DEC88>
>>is_generator(f2)
False
>>>
;-)

- Paddy.

Oct 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
13
by: Emmanuel | last post by:
Hi, I run across this problem, and couldn't find any solution (python 2.2.2) : Code : =========== from __future__ import generators >>> class titi:
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
7
by: simonwittber | last post by:
>>> gen = iterator() >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next <method-wrapper object at 0x009D1BB0> >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
11
by: vbgunz | last post by:
I am afraid that this is the first time in which I would probably need something explained to me as if I were a little child. I am having a hard time getting this through my thick skull. What in...
3
by: Paul Rubin | last post by:
As I understand it, generators are supposed to run til they hit a yield statement: import time def f(): print 1 time.sleep(3) for i in range(2,5): yield i
2
by: Bernhard Mulder | last post by:
I have something like the following (using Python 2.5 syntax): class adapt(object): def __init__(self): self.iterator = self.generator() self.n = 0 def generator(self): while True: while...
6
by: python | last post by:
Is there an elegant way to unget a line when reading from a file/stream iterator/generator? By "unget" I mean a way to push back a line into the source stream and backtrack the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.