Robert wrote:
> I polished off some more of the pure python mode stuff tonight. I
>just pushed it to cython-devel. Now you can do stuff like:
>
Excellent! I have some comments to the syntax of course :-)
Do you see this as the way forward for all new Cython code, or just when one
has interpreter compatability requirements? I suppose Sage will want to stay
consistent with the current code, while for NumPy/numerics one could for
instance rewrite the tutorial etc. to this syntax. It probably is more
cumbersome for daily usage though...
>> import cython # you need a single file to be in your path to use
> it in pure Python mode, provided (and installed) with Cython
>
>> @cython.locals(x=int, y=cython.p_int)
> def foo(x):
> y = cython.cast(cython.p_int, x)
> print x + cython.sizeof(y) + cython.sizeof(cython.p_int)
> if cython.compiled:
> print "the compiler was run
> else:
> print "just being interpreted"
>
>> x = cython.declare(int)
> xx = cython.declare(cython.p_int)
> xx = cython.address(x)
Can you also do something like
from cython import cast
? It doesn't work for directives but I consider that a bug...
As for syntax, I have a problem with the assignment style, i.e. I'd rather see:
cython.declare(x=cython.int)
because it seems more magical, and so one isn't surprised by stuff like this:
x = cython.declare(cython.int)
x = "hello" # illegal? Huh?
On the brainstorming side, I was wondering if perhaps both casting and
declaration could be done via constructor:
x = cython.int(3)
It has the same problem as your cython.declare though so I won't support that
at this stage (but with inference it could work).
Another alternative:
with cython.locals(..): ...
The thing is, "with" sets up a natural scope block, and so it could be possible
to declare variables e.g. inside a for loop with such a construct. A bit
verbose though.
>
>> MyStruct = cython.struct(x=int, y=int, data=cython.pp_double)
>
>> a = cython.declare(MyStruct)
> a.x = 5
>
>> T = cython.typedef(MyStruct)
> b = cython.declare(cython.pointer(T))
> b[0].x = 4
Nice that all of this is working!
Brainstorming again:
I feel that this alternative to typedefs may not be too much work, just
mentioning it:
cdef class A: pass
B = A # both symbol ass. and typedef
@cython.locals(x=B) # type..
def f():
x = B() # module var
i.e. B = A be both a variable assignment *and* a typedef. This would only be
for top-level module assignments, so
if True:
C = A
@cython.locals(x=C) #illegal!
def f():
x = C() # legal
furthermore,
myint = cython.int
would only trigger a typedef as int is not a variable at runtime.
Take it for what it is (a brainstorming); if the assignment style is changed to
statement style for cython.declare and cython.typedef I'm 100 percent happy,
and quite happy for all of this even if it stays like now.
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev