howto overload with a NOP (empty statement)
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sat Jan 6 07:09:16 EST 2007
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 ?
More information about the Python-list
mailing list