455,545 Members | 1,391 Online Need help? Post your question and get tips & solutions from a community of 455,545 IT Pros & Developers. It's quick & easy.

# Type in Python, Pyrex, etc...

 P: n/a Hi, I'm playing with python and try to add type declaration in python. No, don't leave now ! I have done it in order to stay python's syntax compatible (whereas for example pyrex introduce a new syntax) so of course it should not be that nice but... The advantage is that you can debug with the python interpreter and then compile it trough pyrex. Of course, some other program like StarKiller could also use such annotation... First I though to use declare(int,obj) (wich in the interpreter just check type) but it cause exception if the obj is not yet initialize so I change the notation... What do you think about this ? Look at the following example (from pyrex) and the automated translation in pyrex -- Yermat # Prime.py from declaration import * def primes(kmax): declare(ListOf(int),primes) declare(int,kmax) n = int k = int i = int p = ArrayOf(int,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 if __name__=="__main__": print primes(10) # Prime.pyx # automatically generated from the previous one def primes(int kmax): cdef int n cdef int k cdef int i cdef int p 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 if __name__ == '__main__': print primes(10) Jul 18 '05 #1 