abc for generators?

A

Alan Franzoni

Hello,
I was looking for an ABC for generators in python. While there's a
types.GeneratorType type object - which can't be used directly and it's
not an abc - and many collections-related ABCs in the collections
module, there's no way to say a user-defined class as a generator, even
though it could expose the very same interface as a builtin, yield-based
generator.

I think it would be pretty useful.

comments?
 
C

Carl Banks

Hello,
I was looking for an ABC for generators in python. While there's a
types.GeneratorType type object - which can't be used directly  and it's
not an abc - and many collections-related ABCs in the collections
module, there's no way to say a user-defined class as a generator, even
though it could expose the very same interface as a builtin, yield-based
generator.

I think it would be pretty useful.

comments?

collections.Iterator


You don't actually need abc's to write custom iterators, BTW, the
following is an iterator for instance.

class Noise:
def __iter__(self):
return self
def __next__(self):
return random.random()


which is equivalent to the generator


def Noise():
while True:
yield random.random()


Carl Banks
 
A

Alan Franzoni

collections.Iterator

That would just support the next() method. But enhanced generators
interface - supporting coroutines as well - include send(), throw() and
close().
 
C

Carl Banks

That would just support the next() method. But enhanced generators
interface - supporting coroutines as well - include send(), throw() and
close().

Hmm, don't know.

Maybe you could contribute one, it sounds like a rather
unobjectionable idea likely to be accepted into Python.


Carl Banks
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top