Classes

Chris Angelico rosuav at gmail.com
Fri Oct 31 17:56:18 EDT 2014


On Sat, Nov 1, 2014 at 8:40 AM, Rob Gaddi
<rgaddi at technologyhighland.invalid> wrote:
> For this specific exercise, a Square is a subclass of Rectangle because
> the point of Rectangle is to demonstrate that extraneous get/set
> functions are completely unnecessary in Python.  The point of
> Square is to demonstrate that get/set functions can be useful in
> certain circumstances where you need to implement non-trivial behaviors,
> such as making the "width" property into an alias for the "length" true
> data member.

As the Wikipedia article explains, this has its own consequences. A
reasonable test suite for Rectangle would quite probably fail if given
a Square. Hence my belief that this makes for a less-than-ideal
example. But I can't think of *any* good example of @property for a
tutorial. ANY.

> As a learning tool for thinking about inheritance hierarchies it's kind
> of rubbish.  But then again, most inheritance hierarchies are ambigious
> at best, which is why "has a" is often a better choice than "is a".

Agreed. There certainly are times when "is a" is the right choice, but
there are a lot more times when "has a" is the better choice. Usually,
when I subclass, it's because I want to tweak the behaviour of an
existing type (for instance, subclass int and change its repr() to
return hex(self)), so it really truly is a <superclass>, in every way
except that one tiny change. Otherwise, new type with a member. Much
simpler. Much safer.

ChrisA



More information about the Python-list mailing list