[Pyrex] nagging differences / implementing generators

Greg Ewing greg at cosc.canterbury.ac.nz
Fri May 7 03:55:24 CEST 2004


"David J. C. Beach" <beach at verinet.com>:

> 1) +=3D, -=3D, etc. operators
> 2) list comprehensions
> 3) tuples in python functions:   "def x(a, (b,c)): ..."
> 
> This are features that I miss in Pyrex, but see no inherent reason for
> their absence.  Are these features planned for a 1.0 release of Pyrex?=20
> These features are important to me, and I'd like to see them included.=20
> Is there some way that I could help?

They're missing mainly because there are still other things I consider
more important to work on first. Their implementation is
straightforward in principle, although the details could be
tedious. You're welcome to have a go at implementing them if you want,
and I'll consider incorporating anything you come up with.

A word of warning about no. 3, though. Be careful with the code
implementing function parameters, as there are a lot of subtleties to
what it does. To paraphrase Knuth, think thrice before touching
anything there!

> 1) having "cdef int x =3D 3" or "cdef double y =3D 2.2"

Again, it would be fairly straightforward in principle.

> 2) building generators / allowing yield

> My thought is to have a special integer variable in the extension type
> which stores the "position" of the execution within the next()
> function...
> 
> Upon entering the "next" function, the C code should examine the value
> of the "position" variable with a "switch" statement, and do a
> computed - goto to jump to the appropriate place in the function's
> execution.

Yes, I've had similar thoughts myself. I agree it would be
possible, although it would be a much bigger project than any
of the others. Again, you're welcome to have a go.

By the way, I think there's an easier way to to the jump than your
switch-full-of-gotos. You might not be aware, but in C, the cases of a
switch statement don't have to be at the level of the switch statement
itself -- they can be inside other statements nested within the switch
statement.  So you can do something like

  switch (gen->position) {
    case 1:
    /* code for the first yield */]
    case 2:
    for (...) {
      /* code for the print */
      /* code for the second yield */
      case 3: ;
    }
  }

This could simplify things a lot, since you wouldn't have to break the
rest of the code up into blocks with labels between them.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list