Subclass sadness (was Re: [Python-Dev] PEP 285: Adding a bool type)

Aahz aahz at pythoncraft.com
Mon Apr 1 09:24:44 EST 2002


In article <DlTp8.48032$S52.1614804 at news2.tin.it>,
Alex Martelli  <aleax at aleax.it> wrote:
>
>Actually, this specific problem is there, like several others, only
>*for MUTABLE objects*.  Take an "immutable" rectangle:
>
>class ImmutableRectangle(object):
>    def __init__(self, width, heigth):
>        object.__setattr__(self, 'width', width)
>        object.__setattr__(self, 'heighth', heigth)
>    def __setattr__(self, name, value):
>        raise TypeError, "cannot mutate ImmutableRectangle"
>    def __hash__(self):
>        return hash(self.width) ^ hash(self.heigth)
>    def area(self):
>        return self.width*self.heigth
>    def perimeter(self):
>        return 2*(self.width+self.heigth)
>    # etc, etc
>
>Now, subclassing this is perfectly fine:
>
>class ImmutableSquare(ImmutableRectangle):
>    def __init__(self, side):
>        ImmutableRectangle.__init__(self, side, side)

Where is the side attribute?
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?



More information about the Python-list mailing list