Re: Pure virtual functions in Python?

2010-02-25 Thread lallous
On Feb 21, 11:21 am, Lie Ryan lie.1...@gmail.com wrote:
 On 02/21/10 19:27,lallouswrote:
 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.

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-25 Thread lallous
On Feb 22, 12:42 am, 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.

 --
 Greg


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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-25 Thread Jean-Michel Pichavant

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-25 Thread Aahz
In article 38ddd614-583c-430d-b998-214bd6360...@b2g2000yqi.googlegroups.com,
lallous  elias.bachaal...@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 (a...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-24 Thread Jean-Michel Pichavant

Arnaud Delobelle wrote:

lallous elias.bachaal...@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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-21 Thread lallous
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-21 Thread lallous
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-21 Thread Martin v. Loewis
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-21 Thread Lie Ryan
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-21 Thread Gregory Ewing

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Martin v. Loewis
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Diez B. Roggisch

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Martin v. Loewis
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Diez B. Roggisch

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

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Rami Chowdhury
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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Peter Otten
lallous wrote:

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

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

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread I V
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pure virtual functions in Python?

2010-02-20 Thread Arnaud Delobelle
lallous elias.bachaal...@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
-- 
http://mail.python.org/mailman/listinfo/python-list