Define nested class into cython extension class

633 views
Skip to first unread message

Paolo Olivo

unread,
Sep 14, 2011, 4:44:26 AM9/14/11
to cython...@googlegroups.com
Hi!

I am wrapping a c++ class that has nested class, but I cannot expose the nested classes into the cython extension class defined into the .pyx file.

This is the detailed situation:

======
cpp header  (file A.h)
======
class A {
 public:
   struct B {
      int x;
   } ;
   ...
} ;

======
cpp class interface (file a.pxd)
======
cdef extern from "A.h" namespace "A":
   struct B:
      int x

cdef extern from "A.h":
   cdef cppclass A: 
      ...

======
cython extension (file liba.pyx)
======
cimport a

cdef class A(object):
    cdef a.A *cptr
    def __cinit__(self, uri):
        self.cptr = new a.A()
    ...

   class B(object):
      def __init__(self):
         self.x = 0

======
The problem is that when I try to compile I have this error:

   class B(object):
   ^

liba.pyx: class definition not allowed here

======

Is there any way to obtain this structure?
Thanks

Paolo


Robert Bradshaw

unread,
Sep 14, 2011, 1:11:49 PM9/14/11
to cython...@googlegroups.com

No, Cython doesn't yet support nested classes. On that note,
transliterating the C++ API is neither necessary nor always the best
way to provide a Python interface (though sometimes it is).

- Robert

Stefan Behnel

unread,
Sep 14, 2011, 1:39:55 PM9/14/11
to cython...@googlegroups.com
Robert Bradshaw, 14.09.2011 19:11:

> Cython doesn't yet support nested classes.

... within extension types, that is. Nested classes in normal Python
classes are supported.


> On that note,
> transliterating the C++ API is neither necessary nor always the best
> way to provide a Python interface (though sometimes it is).

Agreed.

Stefan

Reply all
Reply to author
Forward
0 new messages