Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pure virtual functions in Python?

2,299 views
Skip to first unread message

lallous

unread,
Feb 20, 2010, 11:12:01 AM2/20/10
to
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"

# Implementation w/o a 'cb', thus 'cb' should not be used
class C2(C1):
def __init__(self):
pass

# Implementation w/ 'cb', thus 'cb' can be used
class C3(C1):
def __init__(self):
pass

def cb(self, param1, param2):
print "i am c3 cb"

# Dispatcher function that calls 'cb' only if 'cb' is implemented in
child classes
def dispatcher(c):
if hasattr(c, 'cb'):
c.cb("Hello", "World")

dispatcher(C2())
dispatcher(C3())

What I want is the ability to have the dispatcher() not to call 'cb'
if it was not implemented in one of the child classes.

Please advise.

Martin v. Loewis

unread,
Feb 20, 2010, 11:46:17 AM2/20/10
to lallous
lallous wrote:
> Hello
>
> How can I do something similar to pure virtual functions in C++ ?

See, for example

http://code.activestate.com/recipes/266468/

Regards,
Martin

Diez B. Roggisch

unread,
Feb 20, 2010, 11:46:42 AM2/20/10
to
Am 20.02.10 17:12, schrieb lallous:

There is nothing more beyond that what you already did. You can raise a
NotImplementedError for classes that don't implement the method. That's it.

Diez

Martin v. Loewis

unread,
Feb 20, 2010, 12:08:33 PM2/20/10
to Diez B. Roggisch
>> class C1:
>>
>> # Pure virtual
>> def cb(self, param1, param2):
>> """
>> This is a callback
>>
>> @param param1: ...
>> @param param2: ...
>> """
>> raise NotImplementedError, "Implement me"
>>
>> # Dispatcher function that calls 'cb' only if 'cb' is implemented in
>> child classes
>> def dispatcher(c):
>> if hasattr(c, 'cb'):
>> c.cb("Hello", "World")
>>
>> dispatcher(C2())
>> dispatcher(C3())
>>
>> What I want is the ability to have the dispatcher() not to call 'cb'
>> if it was not implemented in one of the child classes.
>>
>> Please advise.
>
> There is nothing more beyond that what you already did. You can raise a
> NotImplementedError for classes that don't implement the method. That's it.

That's not true. Currently, the hasattr() call would report that cb is
available, when it is actually not implemented. It would be possible to
do something like

if hasattr(c, 'cb') and not is_pure(c.cb):


c.cb("Hello", "World")

is_pure could, for example, look at a function attribute of the
callback. You'd write something like

@pure_virtual
def cb(self, param1, param2):
not_implemented

Regards,
Martin

Diez B. Roggisch

unread,
Feb 20, 2010, 12:14:04 PM2/20/10
to
Sorry, I totally mis-read the OP, too tired. You are right of course.

Diez

Rami Chowdhury

unread,
Feb 20, 2010, 12:19:27 PM2/20/10
to pytho...@python.org, Diez B. Roggisch

Perhaps you could use an easier-to-ask-forgiveness-than-permission idiom?

def dispatcher(c):
try:


c.cb("Hello", "World")

except NotImplementedError:
pass

----
Rami Chowdhury
"Passion is inversely proportional to the amount of real information
available." -- Benford's Law of Controversy
408-597-7068 (US) / 07875-841-046 (UK) / 01819-245544 (BD)

Peter Otten

unread,
Feb 20, 2010, 1:32:33 PM2/20/10
to
lallous wrote:

> How can I do something similar to pure virtual functions in C++ ?

http://docs.python.org/library/abc.html#abc.abstractmethod

Peter

I V

unread,
Feb 20, 2010, 3:04:21 PM2/20/10
to
On Sat, 20 Feb 2010 08:12:01 -0800, lallous wrote:
> How can I do something similar to pure virtual functions in C++ ?

From what you want, it seems like you want cb() to not be called if it
isn't implemented in the derived class; this isn't really what pure
virtual functions in C++ do - pure virtual functions enforce, at compile
time, that the derived class implements the method.

If you have a situation when you want to either call a derived class's
version of cb(), or do nothing, can you not just have an implementation
of cb() in the base class that does nothing, i.e.

class C1(object):
def cb(self, param1, param2):
pass

Arnaud Delobelle

unread,
Feb 20, 2010, 5:02:44 PM2/20/10
to
lallous <elias.ba...@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?

> # Implementation w/o a 'cb', thus 'cb' should not be used
> class C2(C1):
> def __init__(self):
> pass
>
> # Implementation w/ 'cb', thus 'cb' can be used
> class C3(C1):
> def __init__(self):
> pass
>
> def cb(self, param1, param2):
> print "i am c3 cb"
>
> # Dispatcher function that calls 'cb' only if 'cb' is implemented in
> child classes
> def dispatcher(c):
> if hasattr(c, 'cb'):
> c.cb("Hello", "World")
>
> dispatcher(C2())
> dispatcher(C3())
>
> What I want is the ability to have the dispatcher() not to call 'cb'
> if it was not implemented in one of the child classes.

If you don't define cb in the parent class, it'll work.

--
Arnaud

lallous

unread,
Feb 21, 2010, 3:22:56 AM2/21/10
to

Hello Martine,

Can you elaborate more on how to use the mechanism you described?

Thanks,
Elias

lallous

unread,
Feb 21, 2010, 3:27:24 AM2/21/10
to
Thanks everyone for the answers.

The dispatcher() is actually sits in C++ code.

So my code receives an object that is an instance of the base class,
it PyObject_GetAttrString(py_obj, 'funcname'). If the attribute exists
I will call PyObject_CallMethod on it.

If the base defines the method and it was empty, then my C++ code
would still call the function. This is not optimal because I don't
want to go from C++ to Python if the _derived_ class does not
implement the cb. Now the base class should define it so that doc
parsers properly describe the base class.

The recipe suggested is not worth the trouble.
Unfortunately I cannot use abc module since I use Python 2.5

Martin v. Loewis

unread,
Feb 21, 2010, 4:24:24 AM2/21/10
to lallous
>> That's not true. Currently, the hasattr() call would report that cb is
>> available, when it is actually not implemented. It would be possible to
>> do something like
>>
>> if hasattr(c, 'cb') and not is_pure(c.cb):
>> c.cb("Hello", "World")
>>
>> is_pure could, for example, look at a function attribute of the
>> callback. You'd write something like
>>
>> @pure_virtual
>> def cb(self, param1, param2):
>> not_implemented
>>
>> Regards,
>> Martin
>
> Hello Martine,
>
> Can you elaborate more on how to use the mechanism you described?

There are various ways to do it; the one I had in mind uses function
attributes:

def pure_virtual(func):
func.pure_virtual = True # only presence of attribute matters,
# not value
return func

def is_pure(method): # method might be either a method or a function
try:
func = method.im_func
except AttributeError:
func = method
return hasattr(func, 'pure_virtual')

not_implemented = object() # could also write pass instead, or raise

HTH,
Martin

Lie Ryan

unread,
Feb 21, 2010, 5:21:44 AM2/21/10
to
On 02/21/10 19:27, lallous wrote:
<snip>

> If the base defines the method and it was empty, then my C++ code
> would still call the function. This is not optimal because I don't
> want to go from C++ to Python if the _derived_ class does not
> implement the cb.

That sounds like a microoptimization; have you profiled your code and
determined that calling empty function causes a bottleneck? I doubt it.

> Now the base class should define it so that doc
> parsers properly describe the base class.

> The recipe suggested is not worth the trouble.
> Unfortunately I cannot use abc module since I use Python 2.5

Because nobody here could have guessed that your dispatcher was written
in C++; your problem is near trivial if your dispatcher is a pure-python
code.

Gregory Ewing

unread,
Feb 21, 2010, 6:42:53 PM2/21/10
to
lallous wrote:
> If the base defines the method and it was empty, then my C++ code
> would still call the function. This is not optimal because I don't
> want to go from C++ to Python if the _derived_ class does not
> implement the cb.

I would simply not implement the method at all in the base
class. Then the C++ code can do an attribute lookup for
the method, and if it's not found, do nothing.

--
Greg

Jean-Michel Pichavant

unread,
Feb 24, 2010, 4:17:23 AM2/24/10
to Arnaud Delobelle, pytho...@python.org
Arnaud Delobelle wrote:
> lallous <elias.ba...@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

lallous

unread,
Feb 25, 2010, 7:25:14 AM2/25/10
to
On Feb 21, 11:21 am, Lie Ryan <lie.1...@gmail.com> wrote:
> On 02/21/10 19:27,lallouswrote:

You are right. I haven't checked how much it costs to continuously
call an empty function, but why do it if I know (during initialization
from my C++ dispatcher code) that certain Python object should not
have certain methods called.

I still prefer not to call at all, even if it was an empty function.

Regards,
Elias

lallous

unread,
Feb 25, 2010, 7:25:48 AM2/25/10
to
On Feb 22, 12:42 am, Gregory Ewing <greg.ew...@canterbury.ac.nz>
wrote:
> lallouswrote:


That is what I currently do. But if I comment out the implementations
(empty ones) then the documentation generation tool will not document
the callbacks.

Jean-Michel Pichavant

unread,
Feb 25, 2010, 9:18:38 AM2/25/10
to lallous, pytho...@python.org
lallous wrote:
> I still prefer not to call at all, even if it was an empty function.
>
> Regards,
> Elias
>

Is there any way we could convince you that there is no point caring
about this ? Even if you were trying to optimize speed, it would still
require proof that an empty function is part of the problem.
It sounds like someone stating "I prefer to write difficult-to-read
code, because in 1978, code size used to matter".

JM

Aahz

unread,
Feb 25, 2010, 3:32:19 PM2/25/10
to
In article <38ddd614-583c-430d...@b2g2000yqi.googlegroups.com>,
lallous <elias.ba...@gmail.com> wrote:
>On Feb 22, 12:42=A0am, Gregory Ewing <greg.ew...@canterbury.ac.nz>

>wrote:
>> lallouswrote:
>>>
>>> If the base defines the method and it was empty, then my C++ code
>>> would still call the function. This is not optimal because I don't
>>> want to go from C++ to Python if the _derived_ class does not
>>> implement the cb.
>>
>> I would simply not implement the method at all in the base
>> class. Then the C++ code can do an attribute lookup for
>> the method, and if it's not found, do nothing.
>
>That is what I currently do. But if I comment out the implementations
>(empty ones) then the documentation generation tool will not document
>the callbacks.

Maybe deleting the method after the class would work.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer

0 new messages