Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pure virtual functions in Python?

Reply
Thread Tools

Pure virtual functions in Python?

 
 
lallous
Guest
Posts: n/a
 
      02-20-2010
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.
 
Reply With Quote
 
 
 
 
Martin v. Loewis
Guest
Posts: n/a
 
      02-20-2010
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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-20-2010
Am 20.02.10 17:12, schrieb lallous:
> 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.


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
 
Reply With Quote
 
Martin v. Loewis
Guest
Posts: n/a
 
      02-20-2010
>> 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
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-20-2010
Sorry, I totally mis-read the OP, too tired. You are right of course.

Diez
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      02-20-2010
On Saturday 20 February 2010 11:46:42 Diez B. Roggisch wrote:
> Am 20.02.10 17:12, schrieb lallous:
> > 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.

>
> 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
>


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)
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-20-2010
lallous wrote:

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


http://docs.python.org/library/abc.h...abstractmethod

Peter
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      02-20-2010
lallous <(E-Mail Removed)> 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
 
Reply With Quote
 
lallous
Guest
Posts: n/a
 
      02-21-2010
On Feb 20, 6:08*pm, "Martin v. Loewis" <(E-Mail Removed)> wrote:
> >> 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


Hello Martine,

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

Thanks,
Elias
 
Reply With Quote
 
lallous
Guest
Posts: n/a
 
      02-21-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need clarification for virtual method and pure virtual function a C++ 7 06-28-2007 12:38 PM
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments