Is this Pythonic?

P

phil hunt

Suppose I'm writing an abstract superclass which will have some
concrete subclasses. I want to signal in my code that the subclasses
will implement certan methods. Is this a Pythonic way of doing what
I have in mind:

class Foo: # abstract superclass
def bar(self):
raise Exception, "Implemented by subclass"
def baz(self):
raise Exception, "Implemented by subclass"

class Concrete(Foo):
def bar(self):
#...actual implemtation...
def baz(self):
#...actual implemtation...
 
P

Peter Hansen

phil said:
Suppose I'm writing an abstract superclass which will have some
concrete subclasses. I want to signal in my code that the subclasses
will implement certan methods. Is this a Pythonic way of doing what
I have in mind:

class Foo: # abstract superclass
def bar(self):
raise Exception, "Implemented by subclass"
def baz(self):
raise Exception, "Implemented by subclass"

Change those to "raise NotImplementedError('blah')" instead and you'll
be taking the more idiomatic approach.

-Peter
 
C

Caleb Hattingh

Peter

To my mind, this kind of setup (interface class, or abstact class) is more
usually used in static languages to benefit polymorphism - but python is
dynamically typed, so in which situations would this setup be useful in a
python program? You see, I expected your post to say that it wouldn't
even be necessary, but you didn't :)

I have spent a little effort training myself not to bother setting up
class hierarchies like this in python, due to the fact that I use Delphi a
lot at work (I do pretty much the code below to let myself know when an
inherited/abstract class method is being called in error).

regards
Caleb
 
B

Bruno Desthuilliers

Caleb Hattingh a écrit :
Peter

To my mind, this kind of setup (interface class, or abstact class

are two different things.
) is
more usually used in static languages
True.

>to benefit polymorphism

This is a good reason to use an interface in Java. C++ has no notion of
'interface', so you have to use abstract classes to achieve the same result.
- but
python is dynamically typed, so in which situations would this setup be
useful in a python program?

Abstract classes ? When you want to factor out common implementation in
a base class and force derived class to implement specific parts. One
common use case is the template method (aka "hollywood", aka "don't call
us, we'll call you") pattern.
You see, I expected your post to say that
it wouldn't even be necessary, but you didn't :)

Implementation inheritance is never "necessary". Nor are OO, modularity,
structured programming, and human-readable programming languages !-)

It's a fact that inheritence being "only" (err... should I say "mostly"
?) an implementation mechanism in dynamic languages, class hierarchies
tends to be much more flat. But this doesn't mean that abstract base
classes are useless.
I have spent a little effort training myself not to bother setting up
class hierarchies like this in python

I had this pattern too.
 
E

Erik Max Francis

phil said:
Suppose I'm writing an abstract superclass which will have some
concrete subclasses. I want to signal in my code that the subclasses
will implement certan methods. Is this a Pythonic way of doing what
I have in mind:

class Foo: # abstract superclass
def bar(self):
raise Exception, "Implemented by subclass"
def baz(self):
raise Exception, "Implemented by subclass"

class Concrete(Foo):
def bar(self):
#...actual implemtation...
def baz(self):
#...actual implemtation...

Yes, but raise NotImplementedError instead of Exception. Another trick
you can use is to prevent people from instantiating the abstract class:

class Foo:
def __init__(self):
if self.__class__ is Foo:
raise NotImplementedError
...

def bar(self):
raise NotImplementedError
 
P

phil hunt

Peter

To my mind, this kind of setup (interface class, or abstact class) is more
usually used in static languages to benefit polymorphism - but python is
dynamically typed, so in which situations would this setup be useful in a
python program? You see, I expected your post to say that it wouldn't
even be necessary, but you didn't :)

I realise it's not necessary. I just thought it would be nice to
document the interfaces my concrete classes will be using.
I have spent a little effort training myself not to bother setting up
class hierarchies like this in python, due to the fact that I use Delphi a
lot at work (I do pretty much the code below to let myself know when an
inherited/abstract class method is being called in error).

I started doing OO stuff with Smalltalk, where it isn't necessary,
then moved to C++, where it is, and liked it.
 
P

phil hunt

Yes, but raise NotImplementedError instead of Exception. Another trick
you can use is to prevent people from instantiating the abstract class:

class Foo:
def __init__(self):
if self.__class__ is Foo:
raise NotImplementedError
...

def bar(self):
raise NotImplementedError

That's a clever trick, but it's obvious from the code that the class
is intended to be abstract, so if people are stupid enough to shoot
themselves in the foot by creating an instance, I don't feel like
adding extra code to protect themselves from their stupidity.
 
E

Erik Max Francis

phil said:
That's a clever trick, but it's obvious from the code that the class
is intended to be abstract, so if people are stupid enough to shoot
themselves in the foot by creating an instance, I don't feel like
adding extra code to protect themselves from their stupidity.

Right. But even if you're not worried about stupidity, it's useful to
have it fail in an explicit way as early as possible, rather than later
on. With that addition, the moment you try to create an instance of an
abstract class, you get an exception. Even if it's just a typo, and not
severe negligence, that helps the problem get fixed sooner, rather than
later.
 
M

Michael Hudson

Suppose I'm writing an abstract superclass which will have some
concrete subclasses. I want to signal in my code that the subclasses
will implement certan methods. Is this a Pythonic way of doing what
I have in mind:

class Foo: # abstract superclass
def bar(self):
raise Exception, "Implemented by subclass"
def baz(self):
raise Exception, "Implemented by subclass"

class Concrete(Foo):
def bar(self):
#...actual implemtation...
def baz(self):
#...actual implemtation...

Well, I guess you know this, but if Foo contains no implementation at
all, why inherit from it? It would (possibly) be more Pythonic to
define an interface instead, or just use duck typing.

Cheers,
mwh
 
P

phil hunt

Well, I guess you know this, but if Foo contains no implementation at
all, why inherit from it?

(in fact the class I'm using/creating does contain some
implementation)
It would (possibly) be more Pythonic to
define an interface instead,

Does Python have the concept of an interface? When was that added?
 
M

Michael Hudson

Does Python have the concept of an interface? When was that added?

It doesn't have one included, but there are at least two
implementations, zope.interface and PyProtocols (I'm sure google will
find you more on both of these).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top