[Python-3000] PEP 3124 - Overloading, Generic Functions, Interfaces, etc.

Steven Bethard steven.bethard at gmail.com
Wed May 9 22:16:26 CEST 2007


On 5/9/07, BJörn Lindqvist <bjourne at gmail.com> wrote:
> I very much prefer the latter version. The reason is because the
> "locality of reference" is much worse in the overloaded version and
> because I have found it to be very hard to read and understand
> overloaded code in practice.
>
> Let's say you find some code that looks like this:
>
>     def do_stuff(ob):
>         yield obj
>
>     @overload
>     def do_stuff(ob : ClassA):
>         for o in ob:
>             for ob in do_stuff(o):
>                 yield ob
>
>     @overload
>     def do_stuff(ob : classb):
>         yield ob
>
> Or this:
>
>     def do_stuff(ob):
>         if isinstance(ob, classb) or not isinstance(ob, ClassA):
>             yield ob
>         else:
>             for o in ob:
>                 for ob in do_stuff(o):
>                     yield ob
>
> With the overloaded code, you have to read EVERY definition of
> "do_stuff" to understand what the code does. Not just every definition
> in the same module, but every definition in the whole program because
> someone might have extended the do_stuff generic function.

I don't buy this argument.  That's like saying that I can't understand
what len() does without examining every object that defines __len__().
 Do you really have trouble understanding functions like len() or
hash()?

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list