Re: [cython-users] pure virtual methods (and/or abstract classes)

1,266 views
Skip to first unread message

Robert Bradshaw

unread,
Dec 20, 2012, 1:43:03 PM12/20/12
to cython...@googlegroups.com
Nope, it's just like Python.

On Thu, Dec 20, 2012 at 10:32 AM, Дмитро Бєлєвцов <belev...@gmail.com> wrote:
> I'm trying to use .pxd file as an interface. I don't want to create and
> compile corresponding .pyx file because the definitions inside .pxd are only
> supposed to be inherited, but not instantiated. In C++, one would declare a
> class with pure virtual methods in order to accomplish this.
>
> interface.pxd:
>
> cdef class IModel:
> cdef void do_smth(self)
>
> impl.pyx:
>
> from interface cimport IModel
>
> cdef class A(IModel):
> cdef void do_smth(self):
> pass
>
>
> Everything nicely compiles, but when I'm importing impl.so in python I get
> "ImportError: No module named interface"
> Apparently python looks for IModel's implementation in the form of
> "interfaces.so". Is there a way around it without creating and compiling
> useless "interfaces.so?"
>
>
> Thanks!

Stefan Behnel

unread,
Dec 21, 2012, 3:06:25 AM12/21/12
to cython...@googlegroups.com
[fixed citation order]

Дмитро Бєлєвцов, 21.12.2012 02:16:
> Четвер, 20 грудня 2012 р. 19:43:03 UTC+1 користувач Robert Bradshaw написав:
>> On Thu, Dec 20, 2012 at 10:32 AM, Дмитро Бєлєвцов wrote:
>>> I'm trying to use .pxd file as an interface. I don't want to create and
>>> compile corresponding .pyx file because the definitions inside .pxd are
>>> only
>>> supposed to be inherited, but not instantiated. In C++, one would
>>> declare a
>>> class with pure virtual methods in order to accomplish this.
>>>
>>> interface.pxd:
>>>
>>> cdef class IModel:
>>> cdef void do_smth(self)
>>>
>>> impl.pyx:
>>>
>>> from interface cimport IModel
>>>
>>> cdef class A(IModel):
>>> cdef void do_smth(self):
>>> pass
>>>
>>> Everything nicely compiles, but when I'm importing impl.so in python I
>>> get "ImportError: No module named interface"
>>> Apparently python looks for IModel's implementation in the form of
>>> "interfaces.so". Is there a way around it without creating and compiling
>>> useless "interfaces.so?"
>>
>> Nope, it's just like Python.
>
> Hm, that is very unfortunate.

Why?

> Is there any other elegant way to share
> interface between modules? If not, it might be a nice feature to have in
> cython, because it is a nice way to work with several fast cython
> extensions without leaving a comfortable python shell. For a use case, see
> the detailed section of my very same question on the stackoverflow:
> http://stackoverflow.com/questions/13976809/abstract-classes-with-pure-virtual-functions-in-cython

What makes you think that a virtual method is better than a method that
raises NotImplementedError?

Stefan

Dag Sverre Seljebotn

unread,
Dec 21, 2012, 3:57:27 AM12/21/12
to cython...@googlegroups.com
I think this is a plea for (Java-style) multiple inheritance.

Dag Sverre

Robert Bradshaw

unread,
Dec 21, 2012, 4:54:23 AM12/21/12
to cython...@googlegroups.com
Yep, or even just java abstract classes. From the linked post:

> The worst thing is that this useless dummy implementation has to be compiled and linked so that other cython classes can inherit from it.

Honestly, I don't think that's really so bad. You can even refuse to
instantiate this object by throwing an error in __cinit__ if __class__
is exactly equal to the abstract base class. You still get "nice way
to work with several fast cython extensions without leaving a
comfortable python shell."

On this note, several of us have (independently even) thought about
adding go-style interfaces to Cython, which are even more dynamic.

- Robert

Robert Bradshaw

unread,
Dec 21, 2012, 1:25:43 PM12/21/12
to cython...@googlegroups.com
On Fri, Dec 21, 2012 at 7:10 AM, Дмитро Бєлєвцов <belev...@gmail.com> wrote:
> @Stefan, pure virtual methods (i.e. "virtual void method() = 0")

[As a total aside, speaking of ugliness, I always wonder who came up
with that horrendous syntax for C++.]

> don't need
> to be implemented in the base class. If it was possible to create these in
> cython, one wouldn't have to compile base.so, which contains no useful
> information anyway. In my case, I need an efficient small program to try out
> various modifications of different neuron models, no big deal, so having
> interfaces will considerably reduce the clutter in the code, comparing to
> normal inheritance.

Even when we get interfaces, that doesn't mean there won't be a
corresponding .so file. (Where else would you put the common interface
type information?) Java produces .class files for its interfaces, and
the only reason you don't see one in C++ is that it's entirely
contained in the header file (so compiled anew by each source
#including it, nothing to do with it being virtual (e.g any template
class must be done in this way)). pxd files are not header files.

> @Robert
>> I don't think that's really so bad
>
> Well, fair enough, this is not a critical issue and it certainly doesn't
> prevent me from getting desired behavior, It's just that this little
> base_class.so is extremely annoying, because it never gets directly imported
> in python, neither the interface implementation which it contains ever gets
> used. Anyhow, I'll then try to take advantage of base class' this
> implementation and add some common helper methods there.
>
>> On this note, several of us have (independently even) thought about
> adding go-style interfaces to Cython, which are even more dynamic.
>
> Yeah, java or go-style interfaces will be highly appreciated! I'd love to
> contribute, I'm not sure though if I'm competent enough to do so. Has anyone
> already started working on this?

No one's started as far as I know, though it would be a fairly involved change.

> Пʼятниця, 21 грудня 2012 р. 10:54:23 UTC+1 користувач Robert Bradshaw
Reply all
Reply to author
Forward
0 new messages