[Tutor] calling a superclass method after overriding it
Alan Gauld
alan.gauld at btinternet.com
Wed Sep 23 01:33:52 CEST 2009
"Dave Angel" <davea at ieee.org> wrote
>> def result_of_SPECIALIZED_SQLcall_for_child():
>> name =None
>> return name
I assume this is a placeholder for more complex code to follow?
>> class Child(Parent):
>> def add_name(self):
>> name = result_of_SPECIALIZED_SQLcall_for_child()
>> try:
>> name + ''
This does nothing except raise a type error. But that's
a very contrived way of checking the type.
>> self.name =ame
I assume this is a typo? And it never gets executed because
the error is always raised.
>> except TypeError:
>> super(Child, self).add_name()
> I know this is a simplified example, but I'd still like to point out
> that using exceptions when there's a simple test is not reasonable.
> You can just check for None with a simple if test.
I don't mind using exceptions for a simple test but not where the
test is being forced by a piece of code that does nothing. In this
case I agree it would be better to check explicitly for a string if
a string is required.
Even using exceptions it would be better to do someting like:
try:
if type(name) != str:
raise TypeError
# other stuff here
except TypeError:
super(....)
It makes the purpose of the code obvious.
But
if type(name) != str:
super(....)
else:
# other stuff here
is clearer still
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list