[Pyrex] Python type syntax for Pyrex?

Phillip J. Eby pje at telecommunity.com
Fri Jan 7 19:06:22 CET 2005


Guido's latest type syntax proposal at 
http://www.artima.com/weblogs/viewpost.jsp?thread=87182 looks almost like 
it might be suitable for use in a future version of Pyrex in place of 
cdef+type prefixes.

Some hypothetical examples:

class Spam:
     amount: private[int] = 0
     foo: readonly[str] = "foo!"

     # etc.


class Grail(struct):
     age: int
     volume: float

class Food(union):
     spam: ptr[char]
     eggs: ptr[float]

class CheeseState(enum):
     hard = 1
     soft = 2
     runny = 3



@extern("stdio.h")
def fopen(filename: ptr[char], mode: ptr[char]) -> ptr[FILE]:
     """Open a file"""


class PyAPI(extern("Python.h"))

     @cname("PyObject_Length")
     def len(ob) -> exc[int,-1]:
         """Get the length of an object"""

print PyAPI.len("foo")


It's interesting to think about, and to wonder if ctypes and Pyrex could 
converge on a common way to "spell" C types as Python type 
expressions.  Then, one could prototype code with ctypes and then compile 
it with Pyrex, or add type expressions to regular Python code and compile 
with Pyrex to enhance performance.

Anyway, not a proposal, just food for interesting thought, and to speculate 
on whether there are any Pyrex features that can't be expressed with the 
new syntax.  That is, is there anything we should be proposing that Guido add?




More information about the Pyrex mailing list