callable virtual method

Nigel Rantor wiggly at wiggly.org
Fri Aug 14 14:53:37 EDT 2009


Jean-Michel Pichavant wrote:
> Nigel Rantor wrote:
>> Jean-Michel Pichavant wrote:
>>>
>>> Your solution will work, for sure. The problem is that it will dumb 
>>> down the Base class interface, multiplying the number of methods by 
>>> 2. This would not be an issue in many cases, in mine there's already 
>>> too much meaningful methods in my class for me to add artificial ones.
>>>
>>> Thanks for the tip anyway.
>>
>> I suggest you reconsider.
>>
>> You asked a question and have been given a standard way of achieving 
>> the desired outcome.
>>
>> It's common in OO to use a Template pattern like this.
>>
>> If you're not interested in finding out how loads of people have 
>> already solved the problem then why ask?
>>
>> The methods that require overriding can be prefixed with an underscore 
>> so that people get a hint that they are an implementation detail 
>> rather than part of the public interface.
>>
>> I don't see your problem, other than a vague aesthetic unease.
>>
>> Regards,
>>
>>   n
> I understand how refuting some obvious solution may look just stupid. 
> You're right, I shouldn't have asked.

I never said it seemed stupid. I was merely curious as to why you'd ask 
a question and ignore solutions.

> By the way I'd like to know if I am I alone to find that
> 
> class Stream:
>    def start
>    def stop
>    def reset
> 
> is better than
> 
> class Stream:
>    def start
>    def _start
>    def stop
>    def _stop
>    def reset
>    def _reset
> 
> (try to figure out with 20+ methods)
> What you call aesthetic may sometimes fall into readability.

Depends on what you mean by "better".

Do you mean pleasing to your eye or performs the task you want it to?

Assuming you are taking the aesthetic viewpoint I think that in this 
case it will depend on how you set out your code.

Realise that all of the underscore methods for your class are 
boilerplate, they simply raise an exception.

They can all be at the end of the file, commented as an entire block to 
be left alone.

Editing the main body of code is then fairly easy, and uncluttered...

e.g.

#
# Stream class blah blah blah
#
class Stream:

     def start

     def stop

     def reset

#
# stubs to be over-ridden in sub-classes, add one for each
# method that requires overriding.
#
     def _start
     def _stop
     def _reset

Regards,

   Nigel

p.s. Please take this in the spirit it is offered. I'm trying to stop 
you from ignoring a good suggestion, not make you feel like a fool.



More information about the Python-list mailing list