By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

howto overload with a NOP (empty statement)

P: n/a
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): ;
Jan 6 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
class Power_Supply (device):
def execute (self):
pass

Stef Mientki wrote:
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): ;
Jan 6 '07 #2

P: n/a
Stef Mientki wrote:
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

Jan 6 '07 #3

P: n/a
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...
</off>
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...
</off>
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 ?
Jan 6 '07 #4

P: n/a
Stef Mientki wrote:
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
Jan 6 '07 #5

P: n/a
Bruno Desthuilliers kirjoitti:
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...
</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
Jan 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.