Typed Python?

Michael Sparks zathras at thwackety.com
Sat Jul 3 07:10:52 EDT 2004


On Sat, 3 Jul 2004, Thomas Reichelt wrote:
> short question: is there any language combining the syntax, flexibility and
> great programming experience of Python with static typing? Is there a
> project to add static typing to Python?

This is almost a word for word description of how I've heard Pyrex
described.

   * http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

It's a language that combines the best elements (IMO) of C and Python.
It's designed for writing extension modules for python, however people
have successfully used it for writing standalone programs - albeit with a
C bootstrap/harness.

It's very pleasant to work work, and you can use distutils to
build/distribute your code.

Example code fragments:
------------------
cdef class vorbissimple:
   cdef ogg_vorbis_context* oggVorbisContext
   cdef source_buffer* sourceBuffer
   cdef decode_buffer* decodeBuffer

   def __init__(self):
      self.sourceBuffer = newSourceBuffer(NULL,BUFSIZE)
      self.oggVorbisContext = newOggVorbisContext()
      self.decodeBuffer = NULL
------------------
def primes(int kmax):
        cdef int n, k, i
        cdef int p[1000]
        result = []
        if kmax > 1000:
                kmax = 1000
        k = 0
        n = 2
        while k < kmax:
                i = 0
                while i < k and n % p[i] <> 0:
                        i = i + 1
                if i == k:
                        p[k] = n
                        k = k + 1
                        result.append(n)
                n = n + 1
        return result
----------------------
There's also an excellent tutorial/guide here:
   * http://ldots.org/pyrex-guide/

HTH,


Michael.





More information about the Python-list mailing list