inheritance and default arguments

Jim Meier jim at dsdd.org
Sun Aug 11 21:55:56 CEST 2002


On Sun, 11 Aug 2002 09:47:55 -0600, Andrew Koenig wrote:

> I want a method in a class hierarchy to have a default argument.  For
> example, I might want the method to deal with a designated substring
> of a string that I pass as an argument, and I want the beginning and
> end of the substring to be 0 and len(s) as defaults.
> 
> I can't do this:
> 
>         class Base(object):
>                 def f(self, s, begin=0, end=len(s)):
>                         ...
> 
>         class Derived(object):
>                 def f(self, s, begin=0, end=len(s)):
>                         ...

In this specific case, a one 'pythonic' way would be to use -1 as your
default value for end, assuming you are going to use slices and string
functions on s.

One suggestion would be to have Derived's f() call Base's f() and leave
the dynamic initialization in Base. This means running the original f()
every time, though, which probably isn't desirable.

-Jim



More information about the Python-list mailing list