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.
See, for example
http://code.activestate.com/recipes/266468/
Regards,
Martin
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
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
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)
> How can I do something similar to pure virtual functions in C++ ?
http://docs.python.org/library/abc.html#abc.abstractmethod
Peter
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
> 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
Hello Martine,
Can you elaborate more on how to use the mechanism you described?
Thanks,
Elias
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
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
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.
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
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
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
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.
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
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