Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80114
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Classes |
| Date | 2014-11-02 09:50 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <m34upd$mpq$2@dont-email.me> (permalink) |
| References | (5 earlier) <mailman.15375.1414796262.18130.python-list@python.org> <2i585a5s6d1aagbh6ot2b88i51o8jhq0db@4ax.com> <et685adhnqlerm642uh9dlhs80lmcmhc05@4ax.com> <mailman.15392.1414857661.18130.python-list@python.org> <g83a5apmbvnfh5bbulfqemqhvjvq57vqnp@4ax.com> |
On Sat, 01 Nov 2014 12:42:10 -0400, Seymore4Head wrote:
> OK Maybe I misunderstood the question.
>
> My answer to you then is ......I don't know. I will have to think about
> it some more.
The question (I thought) was to write a class for Square that inherited a
class Rectangle but imposed on it the additional constraints of a square
over a rectangle, namely that length == width.
To do this, you need to override the inherited methods to set length and
width with new methods to set length and width as follows:
when setting length, also set width equal to length.
when setting width, also set length equal to width.
For bonus points, if your constructor accepts width and length parameters
(from the Rectangle constructor) then detect if they are different and
raise a suitable error.
from math import sqrt
class SquareGeometryError(Exception):
"""The parameters create an illegal geometry for a square"""
pass
class Rectangle:
def __init__(self,length,width):
self.length=length
self.width=width
def area(self):
return self.length*self.width
def perimeter(self):
return 2*self.length+2*self.width
def diagonal(self):
return sqrt(self.length*self.length+self.width*self.width)
def get_width(self):
return self.width
def get_length(self):
return self.length
def set_width(self, width):
self.width = width
def set_length(self, length):
self.length = length
class Square(Rectangle):
_def _init__(self, length, width):
if not length == width:
raise SquareGeometryError("Length must equal width")
self.length = length # or width
self.width = length # or width
def set_width(self, width):
self.length = width
self.width = width
def set_length(self, length):
self.length = length
self.width = length
Note that to make my square, I only need to over-ride those rectangle
methods which allow the setting of length and width to enforce squareness
upon the square. All the other methods of rectangle will work equally
well for the square.
--
Denis McMahon, denismfmcmahon@gmail.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 16:16 -0400
Re: Classes sohcahtoa82@gmail.com - 2014-10-30 13:33 -0700
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:00 -0400
Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-30 13:34 -0700
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:00 -0400
Re: Classes Larry Hudson <orgnut@yahoo.com> - 2014-10-30 14:28 -0700
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:34 -0400
Re: Classes sohcahtoa82@gmail.com - 2014-10-30 18:21 -0700
Re: Classes alister <alister.nospam.ware@ntlworld.com> - 2014-10-31 09:59 +0000
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 10:07 -0400
Re: Classes "ast" <nomail@invalid.com> - 2014-10-31 10:05 +0100
Re: Classes "ast" <nomail@invalid.com> - 2014-10-31 10:18 +0100
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-10-31 20:21 +1100
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 10:05 -0400
Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-31 08:47 -0600
Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 15:49 +0000
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:31 -0400
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 03:37 +1100
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:47 -0400
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 04:02 +1100
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:31 -0400
Re: Classes Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-31 12:39 -0500
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:57 -0400
Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 10:40 -0700
Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-01 11:17 +1300
Re: Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:06 +1100
Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-01 00:12 -0600
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 17:32 +1100
Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-03 00:32 +1300
Re: Classes Dave Angel <davea@davea.name> - 2014-10-31 13:20 -0400
Re: Classes Marko Rauhamaa <marko@pacujo.net> - 2014-10-31 19:35 +0200
Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 17:40 +0000
Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-31 18:26 -0400
Re: Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:09 +1100
Re: Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 04:06 +1100
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:18 -0400
Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 10:43 -0700
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:18 -0400
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:42 -0400
Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-31 18:57 -0400
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 19:22 -0400
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 19:32 -0400
Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-01 12:00 -0400
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-01 12:42 -0400
Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-02 09:50 +0000
Re: Classes Tim Delaney <timothy.c.delaney@gmail.com> - 2014-11-02 21:32 +1100
Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-02 12:27 +0000
Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-02 11:31 -0500
Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-03 03:12 +0000
Re: Classes Dan Sommers <dan@tombstonezero.net> - 2014-11-03 06:29 +0000
Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 01:10 +0000
Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-04 18:30 +1300
Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-03 08:50 -0500
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-04 02:34 +1100
Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-03 07:47 -0700
Re: Classes Larry Hudson <orgnut@yahoo.com> - 2014-11-02 14:03 -0800
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 07:51 +1100
Re: Classes sohcahtoa82@gmail.com - 2014-10-31 14:24 -0700
Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 14:40 -0700
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 08:56 +1100
Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 08:36 +1100
Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 17:43 +0000
Re: Classes Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-31 14:11 +0000
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:49 -0400
Re: Classes "ast" <nomail@invalid.com> - 2014-10-31 19:31 +0100
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:36 -0400
Re: Classes Joel Goldstick <joel.goldstick@gmail.com> - 2014-10-31 14:41 -0400
Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:56 -0400
csiph-web