Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'none:': 0.07; 'inherited': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'width': 0.09; 'def': 0.12; 'length,': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:Classes': 0.16; 'width,': 0.16; 'ignore': 0.16; 'example': 0.22; 'tests': 0.22; 'url:home': 0.24; 'header:X-Complaints-To:1': 0.27; 'that.': 0.31; 'dimensions': 0.31; 'forces': 0.31; 'class': 0.32; 'supposed': 0.32; 'fri,': 0.33; 'skip:_ 10': 0.34; 'created': 0.35; 'operations': 0.35; 'skip:> 10': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'area': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'affect': 0.61; 'length': 0.61; 'matter': 0.61; 'simple': 0.61; "you're": 0.61; "you've": 0.63; 'square': 0.74; 'enforces': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Classes Date: Sat, 01 Nov 2014 12:00:46 -0400 Organization: IISS Elusive Unicorn 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> <2i585a5s6d1aagbh6ot2b88i51o8jhq0db@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-73-119-55.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414857661 news.xs4all.nl 2933 [2001:888:2000:d::a6]:40071 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80093 On Fri, 31 Oct 2014 19:32:13 -0400, Seymore4Head declaimed the following: > >class Rectangle(object): > def __init__(self, length, width=None): > self.length = length > if width is None: > self.width = length > else: > self.width = width > def area(self): > return self.length * self.width > def perimeter(self): > return 2 * (self.length + self.width) > >class Square(Rectangle): > def area(self): > return self.length * self.width > def perimeter(self): > return 2 * (self.length + self.width) > Why did you redefine area and perimeter? Those are supposed to be inherited operations from Rectangle. You've duplicated what should not have been changed, and removed everything that makes a Square a Square. The "assignment" was to create a Square class that enforces the property of "squareness" (length = width, or a simple "side"). What you created does not do that. >a=Rectangle(3,5) >print (a.area()) >print (a.perimeter()) >b=Rectangle(5,7) >print (b.area()) >print (b.perimeter()) >c=Square(4) >print (c.area()) >print (c.perimeter()) You're tests ignore the case of /changing/ side/length/width... Consider what happens if you execute c.length = 5 c.width = 9 Your "square" is now a rectangle... My example forces every change to a "Square" to affect both dimensions equally -- no matter if you set .side, .length, .width, the square will still be a square. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/