[Python-Dev] subclassing builtin data structures

Isaac Schwabacher ischwabacher at wisc.edu
Fri Feb 13 21:37:27 CET 2015


On 15-02-13, Guido van Rossum  wrote:
> Are you willing to wait 10 days for an answer? I'm out of round tuits for a while.

IIUC, the argument is that the Liskov Substitution Principle is a statement about how objects of a subtype behave relative to objects of a supertype, and it doesn't apply to constructors because they aren't behaviors of existing objects. So other overriding methods *should* be able to handle the same inputs that the respective overridden methods do, but constructors don't need to. Even though __init__ is written as an instance method, it seems like it's "morally" a part of the class method __new__ that's only split off for convenience.

If this message is unclear, it's because I don't really understand this myself and I'm trying to articulate my best understanding of what's been said on this thread and those it links to.

ijs

> On Fri, Feb 13, 2015 at 10:22 AM, Alexander Belopolsky <alexander.belopolsky at gmail.com(javascript:main.compose()> wrote:
> 
> > 
> > On Fri, Feb 13, 2015 at 1:19 PM, Alexander Belopolsky <alexander.belopolsky at gmail.com(javascript:main.compose()> wrote:
> > >>
> > >> FWIW you're wrong when you claim that "a constructor is no different from any other method". Someone else should probably explain this (it's an old argument that's been thoroughly settled).
> > >
> > >
> > > Well, the best answer I've got in the past [1] was "ask on python-dev since Guido called the operator overriding expectation." :-)
> > 
> > 
> > And let me repost this bit of history [1]:
> > 
> > Here is the annotated pre-r82065 code:
> > 
> > 39876 gvanrossum def __add__(self, other):
> > 39876 gvanrossum if isinstance(other, timedelta):
> > 39928 gvanrossum return self.__class__(self.__days + other.__days,
> > 39876 gvanrossum self.__seconds + other.__seconds,
> > 39876 gvanrossum self.__microseconds + other.__microseconds)
> > 40207 tim_one return NotImplemented
> > 39876 gvanrossum
> > 
> > 
> > 
> > [1] http://bugs.python.org/issue2267#msg125979
> > 
> > 
> 
> 
> 
> 
> -- 
> --Guido van Rossum (python.org/~guido(http://python.org/~guido))


More information about the Python-Dev mailing list