[Pyrex] variable scope : is that the waited behaviour ?
Stefan Behnel
behnel_ml at gkec.informatik.tu-darmstadt.de
Sun Apr 23 18:02:11 CEST 2006
Hi Cyril,
assuming that you're French, I guess you mean "expected" behaviour.
Cyril Giraudon wrote:
> cdef struct variable:
> int a
> int b
I think I'd use "ctypedef" here, but if this works for you...
> cdef variable var1
> cdef variable var2
>
> def oups():
> var1.a = 1
> var1.b = 2
> var2 = var1
> print var2.a, var2.b
>
> Compilation :
> /usr/local/bin/pyrexc ./test.pyx
> /home/cyril/outils/ng-spice-rework-17/src/./test.pyx:11:9: Cannot
> convert 'variable' to Python object
This behaves just as in Python and is therefore expected. You assign a value
to a variable inside a function, which lets Python and Pyrex assume it's a
local name. Since it has not been declared, Pyrex further assumes it's a local
Python variable.
> ##############################
>
> cdef struct variable:
> int a
> int b
>
> cdef variable var1
> cdef variable var2
>
> def oups():
> global var2
> var1.a = 1
> var1.b = 2
> var2 = var1
> print var2.a, var2.b
>
> The compilation succeeds and the module works fine.
Again, as in Python.
> Is there another way to not use the global statement ?
What is wrong with global? You're accessing a global variable, after all.
Stefan
More information about the Pyrex
mailing list