Inheritance from builtin list and override of methods.

Carsten Haese carsten at
Mon Nov 27 20:41:24 CET 2006

On Mon, 2006-11-27 at 19:14 +0000, OKB (not okblacke) wrote:
> Duncan Booth wrote:
> >> And is there a mechanism in Python that will allow me to override
> >> the operators of a class, for all its occurrences, even the ones
> >> implemented on C built-in objects? 
> > 
> > No.
>     	For what it's worth, which is undoubtedly nothing,


>  this is 
> something that I think needs to change.  All this talk about new-style 
> classes and class-type unification is empty words if you can't override 
> anything on any type without having to know whether it's written in C or 
> Python.

Duncan's "No" response was not referring to overriding in general, it
was referring to the OP's original question which amounted to "Can I
affect the behavior of method Z by overriding methods X and Y".

The inability to influence a method by overriding completely different
methods has nothing to do with whether that method is implemented in C
or Python; it has to with whether the method in question calls the
overridden methods, and in general it won't.

You can change the behavior of a list's sort method by overriding sort.
You can't change the behavior of sort by overriding __getitem__ and
__setitem__, because sort does not call __getitem__ or __setitem__.

Hope this helps,


More information about the Python-list mailing list