Is this Pythonic?

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Tue Aug 2 03:36:50 EDT 2005


Delaney, Timothy (Tim) wrote:
> Peter Hansen wrote:
> 
>> Change those to "raise NotImplementedError('blah')" instead and you'll
>> be taking the more idiomatic approach.
> 
> One thing I've noticed, which I may raise on python-dev ...
> NotImplementedError does *not* play well with super() ...
> 
> class A (object):
>     def test (self):
>         raise NotImplementedError
> 
> class B (object):
>     def test (self):
>         print 'B'
>         super(B, self).test()
> 
> class C (B, A):
>     def test (self):
>         print 'C'
>         super(C, self).test()
> 
> It's actually worse than AttributeError, because the method actually
> exists. In both cases though you need to know when you create the base
> class how it's going to be used to work out whether a super() call is
> needed.
> 
> One option is to do a try: except (AttributeError, NotImplementedError).
> Yuk - talk about hiding errors :(

Hm... one could return NotImplemented from the abstract method. It won't raise
an error immediately, but will certainly be discovered at some point.

Not optimal though.

Reinhold



More information about the Python-list mailing list