Generators through the C API

Discussion in 'Python' started by Lucas P Melo, Jul 31, 2009.

  1. Lucas P Melo

    Lucas P Melo Guest

    Hello, I'm a total noob about the C API. Is there any way to create a
    generator function using the C API? I couldn't find anything like the
    'yield' keyword in it.

    Thanks in advance.
    Lucas P Melo, Jul 31, 2009
    #1
    1. Advertising

  2. Duncan Booth schrieb:
    > Lucas P Melo <> wrote:
    >
    >> Hello, I'm a total noob about the C API. Is there any way to create a
    >> generator function using the C API? I couldn't find anything like the
    >> 'yield' keyword in it.
    >>
    >> Thanks in advance.

    >
    > You define a new type with tp_flags including Py_TPFLAGS_HAVE_ITER.
    > Anything that would be a local variable in your generator needs to become
    > an attribute in the type.
    >
    > The tp_init initialization function should contain all the code up to the
    > first yield, tp_iter should return self and tp_iternext should execute code
    > up to the next yield.


    This is pretty easy to do in Cython (or Pyrex), BTW. Just write a class
    with an __iter__ and __next__ method, and Cython will generate the C-API
    code as expected.

    http://docs.cython.org/docs/special_methods.html#iterators

    Note that Cython doesn't currently support the "yield" statement, but
    that's certainly on the ToDo list.

    http://trac.cython.org/cython_trac/ticket/83

    Stefan
    Stefan Behnel, Aug 7, 2009
    #2
    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. Shlomo Anglister
    Replies:
    1
    Views:
    399
    Default User
    Aug 2, 2004
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    361
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. John123

    Profiling API or Membership API

    John123, Oct 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    John123
    Oct 20, 2006
  4. George2

    platform specific API or C standard API

    George2, Nov 12, 2007, in forum: C Programming
    Replies:
    13
    Views:
    731
    Tor Rustad
    Nov 13, 2007
  5. Timothy Grant
    Replies:
    5
    Views:
    405
    Timothy Grant
    Aug 6, 2008
Loading...

Share This Page