[Tutor] calling a superclass method after overriding it

Dave Angel davea at ieee.org
Wed Sep 23 01:45:18 CEST 2009


Serdar Tumgoren wrote:
>> An "if" test would be more readable, I agree.  But I was trying to
>> apply the "Easier to Ask Permission Forgiveness" style, discussed in
>> the Python Cookbook: ,
>>
>>     
> Err..."Easier to Ask Forgiveness than Permission" approach is what I
> meant (perhaps proving my point about not fully understanding the
> technique!)
>
>   
I certainly understand the concept and use it often.  But the main time 
to use it is when it can avoid interrupting the normal flow for normal 
operation.  For example, if you define a function that doesn't return 
unless it has a valid result (for invalid, it raises an exception), then 
the calling function doesn't need to explicitly check the result.  That 
might permit one to embed the first function call into a more complex 
expression without having to decompose it to test for errors at various 
points.

If the first function were written that way (raise, rather than 
returning None), then we could change from:

    def add_name(self):
        name = result_of_SPECIALIZED_SQLcall_for_child()
        try:
            name + ''
            self.name =ame
        except TypeError:
            #default to the superclass's add_name method
            super(Child, self).add_name()

to:

    def add_name(self):
        try:
            self.name = result_of_SPECIALIZED_SQLcall_for_child()
        except SpecialSQLError:
            #default to the superclass's add_name method
            super(Child, self).add_name()


still not really worth it, but it's getting there.  And notice it's 
shorter, and especially so in the normal path.

DaveA



More information about the Tutor mailing list