Python and C scoping rules differs. Please tell us how this is supposed to
behave:
if a:
cdef int x = 4
else:
cdef double x = 5.3
print x
In Python, x would always be available also after the if-test. It's hard
to create rules that would be consistent and work well with what you
propose.
Dag Sverre
> I am using a development version of cython after the 0.12.1 series
> and I'm trying to wrap some C++ code. I have a templated class that
> can have several instantiations based on the underlying type of the
> array. I want the instantiation to be dependent on some input flags.
> Unfortunately cython is not letting me put a cdef inside an if block.
>
> Since this is supposed to compile to C++ why can't I put a variable
> declaration wherever I darn well please?
As Dag Sverre mentioned, there are issues with scoping rules (among
other things). On a higher level, rather than thinking about writing C+
+ code with Python syntax, it's often more correct to just think of
writing Python code that can create/manipulate/call C++ objects and
functions.
- Robert
In fact, the scoping issues is the reason you are limited to only use
C++ objects through pointers in the first place. The C++ object
construction/destruction rules clashes very much with a Python feel in
this respect.
Dag Sverre
> overhead associated with predeclaring the pointers prior to
> any if then else statements.
>
> I guess also the C++ scoping rules are way different from Python's so
> I suppose it wouldn't make much sense.
>
> On Jun 21, 2:39 pm, Robert Bradshaw <rober...@math.washington.edu>
> wrote:
>> On Jun 18, 2010, at 12:57 PM, seventhunders wrote:
>>
>>> I am using a development version of cython after the 0.12.1 series
>>> and I'm trying to wrap some C++ code. I have a templated class that
>>> can have several instantiations based on the underlying type of the
>>> array. I want the instantiation to be dependent on some input flags.
>>> Unfortunately cython is not letting me put a cdef inside an if block.
>>> Since this is supposed to compile to C++ why can't I put a variable
>>> declaration wherever I darn well please?
>> As Dag Sverre mentioned, there are issues with scoping rules (among
>> other things). On a higher level, rather than thinking about writing C+
>> + code with Python syntax, it's often more correct to just think of
>> writing Python code that can create/manipulate/call C++ objects and
>> functions.
>>
>> - Robert
--
Dag Sverre