Pure virtual functions in Python?

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Feb 24 04:17:23 EST 2010


Arnaud Delobelle wrote:
> lallous <elias.bachaalany at gmail.com> writes:
>
>   
>> Hello
>>
>> How can I do something similar to pure virtual functions in C++ ?
>>
>> Let us consider this:
>>
>> class C1:
>>
>>     # Pure virtual
>>     def cb(self, param1, param2):
>>         """
>>         This is a callback
>>
>>         @param param1: ...
>>         @param param2: ...
>>         """
>>         raise NotImplementedError, "Implement me"
>>     
>
> Why define it if it is virtual?
>   

Slightly off topic but this is often useful when writing interfaces. You 
can then properly document what should any subclass (interface 
implemention) be doing.
The thing is that in case of virtual methods, you *do want* to raise the 
notImplemented exceptions, meaning you've failed to implement all the 
required methods.
Strange thing that the OP want to silently call nothing at all when 
calling a virtual method, he looses all the benefits from a virtual design.
Anyway, I don't deal into code optimization, this is not healthy :-)

JM



More information about the Python-list mailing list