abc for generators?

Discussion in 'Python' started by Alan Franzoni, May 19, 2010.

  1. 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?


    --
    Alan Franzoni
    contact me at public@[mysurname].eu
     
    Alan Franzoni, May 19, 2010
    #1
    1. Advertising

  2. Alan Franzoni

    Carl Banks Guest

    On May 19, 8:42 am, Alan Franzoni <>
    wrote:
    > 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
     
    Carl Banks, May 19, 2010
    #2
    1. Advertising

  3. On 5/19/10 7:24 PM, Carl Banks wrote:
    > collections.Iterator


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


    --
    Alan Franzoni
    contact me at public@[mysurname].eu
     
    Alan Franzoni, May 19, 2010
    #3
  4. Alan Franzoni

    Carl Banks Guest

    On May 19, 2:27 pm, Alan Franzoni <>
    wrote:
    > On 5/19/10 7:24 PM, Carl Banks wrote:
    >
    > > collections.Iterator

    >
    > 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
     
    Carl Banks, May 20, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    934
    Jiong Feng
    Nov 19, 2003
  2. Bruce Sam
    Replies:
    15
    Views:
    8,065
    John C. Bollinger
    Nov 19, 2004
  3. vsgdp

    ABC inheriting from ABC

    vsgdp, Sep 24, 2005, in forum: C++
    Replies:
    1
    Views:
    327
    vsgdp
    Sep 24, 2005
  4. Gunter Henriksen

    x.abc vs x['abc']

    Gunter Henriksen, May 13, 2009, in forum: Python
    Replies:
    1
    Views:
    377
    alex23
    May 15, 2009
  5. Replies:
    4
    Views:
    170
Loading...

Share This Page