Is this Pythonic?

Caleb Hattingh caleb1 at telkomsa.net
Mon Aug 1 16:01:06 EDT 2005


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

On Mon, 01 Aug 2005 18:52:02 +0200, Peter Hansen <peter at engcorp.com> wrote:

> phil hunt wrote:
>> 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




More information about the Python-list mailing list