Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Rob Gaddi Newsgroups: comp.lang.python Subject: Re: Classes Date: Fri, 31 Oct 2014 14:40:45 -0700 Organization: Highland Technology, Inc. Lines: 42 Message-ID: <20141031144045.05965228@rg.highlandtechnology.com> References: <51755at03r0bidjqh3qf0hhpvjr8756ill@4ax.com> <545350c3$0$23449$426a74cc@news.free.fr> <5453c1a6$0$12981$c3e8da3$5496439d@news.astraweb.com> <7pg75alj1qn97hlirk81f7nqd265emgu05@4ax.com> <20141031104319.02e1ca6f@rg.highlandtechnology.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Info: mx02.eternal-september.org; posting-host="a48e804ab85e47f9d2e8748775b64aeb"; logging-data="2050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+JnE7P5zewsFXXUekSh+nz" X-Newsreader: Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Cancel-Lock: sha1:A8xIWdVu2o1GDtW/JEqtx9Cc9/8= Xref: csiph.com comp.lang.python:80060 On Fri, 31 Oct 2014 14:24:11 -0700 (PDT) sohcahtoa82@gmail.com wrote: > On Friday, October 31, 2014 1:51:23 PM UTC-7, Chris Angelico wrote: > > On Sat, Nov 1, 2014 at 4:43 AM, Rob Gaddi > > wrote: > > > Define a Square class, subclassed from Rectangle. Use getters/setters > > > to enforce that the length and width must be equal. Confirm that > > > length and width remain locked, and that perimeter() and area() work > > > correctly. > > > > Here we go again... > > > > http://en.wikipedia.org/wiki/Circle-ellipse_problem > > > > Square and Rectangle are just as "obviously" the other way around in > > the hierarchy, and it's wrong whichever way you do it. Not a good > > example. > > > > ChrisA > > I've never heard of the Circle-Ellipse problem, and my first instinct to Rob's post was to ask, why would you want to sub-class Rectangle into a Square class? A square is just a special case of a Rectangle. Attempting that kind of sub-classing would just cause problems. The only thing you gain is a slight optimization in calculating perimeter by turning two multiplications and an addition into a single multiplication which really wouldn't have an effect on performance unless you were doing that calculation millions of times per second. As a practical tool? Entirely and completely useless, no doubt about it. But it's easily understood as an example, even if the reason that a Square is a subclass of Rectangle is "because I said so". 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 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". -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.