[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Pure virtual functions in Python?

lallous

2/20/2010 4:12:00 PM

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.
18 Answers

Martin v. Loewis

2/20/2010 4:46:00 PM

0

lallous wrote:
> Hello
>
> How can I do something similar to pure virtual functions in C++ ?

See, for example

http://code.activestate.com/recip...

Regards,
Martin

Diez B. Roggisch

2/20/2010 4:47:00 PM

0

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

Martin v. Loewis

2/20/2010 5:09:00 PM

0

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

2/20/2010 5:14:00 PM

0

Sorry, I totally mis-read the OP, too tired. You are right of course.

Diez

Rami Chowdhury

2/20/2010 5:19:00 PM

0

Peter Otten

2/20/2010 6:33:00 PM

0

lallous wrote:

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

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

Peter

I V

2/20/2010 8:04:00 PM

0

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

2/20/2010 10:03:00 PM

0

lallous <elias.bachaalany@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

2/21/2010 8:23:00 AM

0

On Feb 20, 6:08 pm, "Martin v. Loewis" <mar...@v.loewis.de> 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

lallous

2/21/2010 8:27:00 AM

0

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