[Numpy-discussion] Where does _assert_func come from.

Fernando Perez fperez.net@gmail....
Thu Aug 28 20:54:37 CDT 2008


On Thu, Aug 28, 2008 at 6:39 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:

> Turns out it's defined in a derived class, which I suppose makes it a mix-in
> class. Looks kinda like the python version of pure virtual functions in C++
> base classes. It's a bit surprising to see a python pattern that's more
> obscure than C++ ;) I'm not sure I like its usage here -- implementation
> rather than is a -- but I'm not going to clean it up.

The typical way to do this in python would be to write in the base
class you saw:

def assert_func(self,a,b):
  "some explanation"
  raise NotImplementedError

this makes the desired interface explicit, while indicating that the
method is meant to be implemented by subclasses.  In a multiple
inheritance situation, however, this may be a bad idea if the class is
meant to be mixed with another that provides the method (because it
could overwrite the real one, depending on how the inheritance diagram
is set up).

In summary, my take on this is:

- if it's meant to be an abstract base class that declares an
interface, use the idiom above for clarity.

- if you're writing a mixin meant to be overlaid on top of another
class with a specific interface like this, then at the very least
document that fact very well in the code.

Just my 1e-2,

f


More information about the Numpy-discussion mailing list