howto overload with a NOP (empty statement)

S

Stef Mientki

How should I overload / disable a method ?
In the example below I have defined the class "Power_Supply", derived
from baseclass "device".
The baseclass has a method "execute", which will be implemented in most
derived classes, but not in all.
Now apparently it's not allowed to overload a method with an empty
statement.
I could write a nonsense dummy statement, like "A= 3", but isn't there
another way ?

thanks, Stef Mientki

class device:
def execute (self):
print 'execute not yet implemented for', self.Name


class Power_Supply (device):
def execute (self): ;
 
R

Robert Kern

Stef said:
How should I overload / disable a method ?
In the example below I have defined the class "Power_Supply", derived
from baseclass "device".
The baseclass has a method "execute", which will be implemented in most
derived classes, but not in all.
Now apparently it's not allowed to overload a method with an empty
statement.
I could write a nonsense dummy statement, like "A= 3", but isn't there
another way ?

pass

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
R

robert

Stef said:
How should I overload / disable a method ?
In the example below I have defined the class "Power_Supply", derived
from baseclass "device".
The baseclass has a method "execute", which will be implemented in most
derived classes, but not in all.
Now apparently it's not allowed to overload a method with an empty
statement.
I could write a nonsense dummy statement, like "A= 3", but isn't there
another way ?

thanks, Stef Mientki

class device:
def execute (self):
print 'execute not yet implemented for', self.Name


class Power_Supply (device):
def execute (self): ;




class device:
def execute (self):
raise NotImplementedError('virtual: should do this and that')



This NotImplementedError virtual method scheme will also be detected by pychecker in order to warn correctly: that classes of actual instances have to override all open virtuals.


Robert
 
B

Bruno Desthuilliers

Stef Mientki a écrit :
How should I overload / disable a method ?
In the example below I have defined the class "Power_Supply", derived
from baseclass "device".

<off>
Naming conventions are to use CamelCase for class names. So it would be
better to name your classes 'PowerSupply' (no '_') and 'Device'. You're
of course free to use whatever naming convention you want, including no
convention at all, but Python relies *a lot* on naming conventions...
The baseclass has a method "execute",

<off>
Do you know that Python let you define your own 'callable' objects ?

class SomeCallable(object):
def __init__(self, name):
self.name = name
def __call__(self):
print "wow, %s has been called" % self.name

foo = SomeCallable('foo')
foo()

This may or not make sens in the context of your application, but
whenever you find yourself naming a method 'execute', it might be worth
asking yourself if the object should in fact be a callable...
which will be implemented in most
derived classes, but not in all.
>
Now apparently it's not allowed to overload a method with an empty
statement.
I could write a nonsense dummy statement, like "A= 3", but isn't there
another way ?

the 'pass' statement

def noop():
pass
thanks, Stef Mientki

class device:
def execute (self):
print 'execute not yet implemented for', self.Name

The usual idiom for 'pure virtual methods' is to raise a
NotImplementedError. Now if it's ok for a subclass to implement the
method as a no-op, why not just implement it as a no-op in the base
class itself ?
 
J

Jussi Salmela

Bruno Desthuilliers kirjoitti:
Stef Mientki a écrit :

<off>
Naming conventions are to use CamelCase for class names. So it would be
better to name your classes 'PowerSupply' (no '_') and 'Device'. You're
of course free to use whatever naming convention you want, including no
convention at all, but Python relies *a lot* on naming conventions...
</off>

Continuing with the style and idioms issues...

1. Don't leave a space between a function/method/class name and the
opening parenthesis, i.e. instead of:
def execute (self):
use:
def execute(self):

2. It seems to me that you indent your code with 2 spaces. If I'm wrong,
please ignore me. Otherwise: always use 4 spaces when indenting with spaces.

Following these and the numerous other guidelines (see
htpp://www.python.org/dev/peps/pep-0008/ for example) on Python style
you'll be making yourself and others a favor. When these coding
guidelines become your habit, it will be easier for you to read the code
of other Pythoners. This is important because a large part of learning
consists of reading the code of others.

And also vice versa: when you show your code to others, it will be
easier for them to concentrate on your problem when the idiosyncrasy of
your code is not there to hinder.

Cheers,
Jussi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top