Main.Shapes History

Hide minor edits - Show changes to markup

April 15, 2013, at 09:37 PM MST by 24.54.128.180 -
Changed line 37 from:

rect3.set_width(5)

to:

rect3.width = 3

Changed lines 44-62 from:
  1. Getters and setters
  2. Here's an alternative version of the Rectangle class:

class Rectangle :

    """A class that represents a rectangle"""
    def __init__(self, width=1, height=1) :
        """width - the width of the rectangle
        height - the height of the rectangle"""
        self.width = width
        self.height = height
    def get_width(self) :
        return self.width
    def set_width(width) :
        self.width = width
    def get_height(self) :
        return self.height
    def set_height(height) :
        self.height = height
to:
  1. Let's define another type of shape:

import math class Circle :

    """A class that represents a circle"""
    def __init__(self, radius=1) :
        """radius - the radius of the circle"""
        self.radius = radius
Changed line 53 from:
        return Rectangle(self.width, self.height)
to:
        return Circle(self.radius)
Changed line 56 from:
        return self.width * self.height
to:
        return math.pi * self.radius * self.radius
Changed line 59 from:
        return 2 * self.width + 2 * self.height
to:
        return 2 * math.pi * self.radius
Changed lines 62-63 from:
        self.height *= s
        self.width *= s
to:
        self.radius *= s
Changed lines 64-95 from:
        return "Rectangle with width = f" % (self.width, self.height)
  1. This version includes "getter" and "setter" methods for accessing and modifying
  2. the object's data.
  3. Why the get and set methods? Afterall, the user can set those by himself
  4. without having to use these methods:
  5. You can easily set rectangle height by: rect.height = 5
  6. In java there is good reason to use such methods but not in python.
  7. See for example: http://tomayko.com/writings/getters-setters-fuxors
  8. Let's define another type of shape:

import math class Circle :

    """A class that represents a circle"""
    def __init__(self, radius=1) :
        """radius - the radius of the circle"""
        self.radius = radius
    def duplicate(self) :
        return Circle(self.radius)
    def area(self) :
        """returns the area of the rectangle"""
        return math.pi * self.radius * self.radius
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * math.pi * self.radius
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.radius *= s
    def __repr__(self) :
        return "Rectangle with radius =  self.radius
to:
        return "Circle with radius =  self.radius
April 04, 2010, at 10:28 PM MST by 71.196.160.210 -
Changed lines 4-5 from:

Classes and objects http://openbookproject.net/thinkcs/python/english2e/ch13.html

to:

More object oriented programming. http://openbookproject.net/thinkcs/python/english2e/ch15.html see also http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm

Changed lines 9-52 from:
  1. Let's write a class that represent the mathematical notion of
  2. a point in two dimensions.
  3. A point is associated with two coordinates, and let's call them
  4. x and y.
  5. We could just store the two values in a list or a tuple.
  6. But when we put them in an object we can also give the object
  7. methods that are appropriate for a two dimensional point.
  8. Here's the simplest way to define a class:

class Point :

    pass
  1. Let's create an instance of the class:

p = Point()

  1. we just called the constructor of the Point class.
  2. Attributes
  3. Attributes are the data that are associated with an object
  4. We can add/access attributes using the dot notation:

p.x = 3 p.y = 4

  1. We can now do things with objects of this class:

def distance_to_origin(p) :

    """compute the distance of a point from the origin"""
    import math
    return math.sqrt(p.x*p.x + p.y *p.y)
  1. now we can do:

print distance_to_origin(p)

  1. Issues:
  2. we want to be able to do p.distance() as opposed to calling it
  3. as a regular function, i.e. we want distance to be a method of Point.
  4. How do we make sure that a point has x and y attributes?
  5. Let's redefine the Point class:
to:

class Rectangle :

    """A class that represents a rectangle"""
    def __init__(self, width=1, height=1) :
        """width - the width of the rectangle
        height - the height of the rectangle"""
        self.width = width
        self.height = height
    def duplicate(self) :
        return Rectangle(self.width, self.height)
    def area(self) :
        """returns the area of the rectangle"""
        return self.width * self.height
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * self.width + 2 * self.height
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.height *= s
        self.width *= s
    def __repr__(self) :
        return "Rectangle with width = f" % (self.width, self.height)

rect1 = Rectangle(1, 2) rect2 = Rectangle()

rect3 = rect1

  1. Let's modify rect3 now:

rect3.set_width(5) print rect3 print rect1

  1. Modifying rect3 has changed rect1! That is because they both
  2. point to the same Rectangle object.
  3. Getters and setters
  4. Here's an alternative version of the Rectangle class:

class Rectangle :

    """A class that represents a rectangle"""
    def __init__(self, width=1, height=1) :
        """width - the width of the rectangle
        height - the height of the rectangle"""
        self.width = width
        self.height = height
    def get_width(self) :
        return self.width
    def set_width(width) :
        self.width = width
    def get_height(self) :
        return self.height
    def set_height(height) :
        self.height = height
    def duplicate(self) :
        return Rectangle(self.width, self.height)
    def area(self) :
        """returns the area of the rectangle"""
        return self.width * self.height
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * self.width + 2 * self.height
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.height *= s
        self.width *= s
    def __repr__(self) :
        return "Rectangle with width = f" % (self.width, self.height)
  1. This version includes "getter" and "setter" methods for accessing and modifying
  2. the object's data.
  3. Why the get and set methods? Afterall, the user can set those by himself
  4. without having to use these methods:
  5. You can easily set rectangle height by: rect.height = 5
  6. In java there is good reason to use such methods but not in python.
  7. See for example: http://tomayko.com/writings/getters-setters-fuxors
  8. Let's define another type of shape:
Changed lines 90-136 from:

class Point :

    def __init__(self, x, y) :
        self.x = x
        self.y = y
    def distance_to_origin(self) :
        """compute the distance of a point from the origin"""
        return math.sqrt(self.x*self.x + self.y *self.y)
    def distance(self, other) :
        """compute the distance between a point object and another
        point object"""
        return math.sqrt((self.x - other.x)**2 + 
                         (self.y - other.y)**2)

p1 = Point(1, 2) p2 = Point(4, 6) print p1.distance(p2)

  1. let's print the objects:

print p1 print p2

  1. the result is not very informative.
  2. let's fix that using the __str__ method:

import math class Point :

    """Point is a class that stores a two dimensional point"""
    def __init__(self, x=0, y=0) :
        self.x = x
        self.y = y
    def distance_to_origin(self) :
        """compute the distance of a point from the origin"""
        return math.sqrt(self.x*self.x + self.y *self.y)
    def distance(self, other) :
        """compute the distance between a point object and another
        point object"""
        return math.sqrt((self.x - other.x)**2 + 
                         (self.y - other.y)**2)
    def __str__(self) :
        return "Point object with x = d" % (self.x, self.y)

p1 = Point(1, 2) p2 = Point(4, 6)

  1. printing the ojbects produces a more informative result:

print p1 print p2

  1. As a rule, whenever you write a classe you should also write
  2. an __str__ method - it's very useful for debugging!
to:

class Circle :

    """A class that represents a circle"""
    def __init__(self, radius=1) :
        """radius - the radius of the circle"""
        self.radius = radius
    def duplicate(self) :
        return Circle(self.radius)
    def area(self) :
        """returns the area of the rectangle"""
        return math.pi * self.radius * self.radius
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * math.pi * self.radius
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.radius *= s
    def __repr__(self) :
        return "Rectangle with radius =  self.radius

circ1 = Circle(2) circ2 = Circle()

  1. Note that both classes present essentially the same interface, i.e.
  2. the same set of methods.
  3. We can exploit this e.g. when using a list of shapes:

shape_list = [Circle(2), Circle(5), Rectangle(1,2), Circle(1), Rectangle(3,5)] for shape in shape_list :

    print "The area is: ", shape.area()
  1. Naming convention: Class names begin with a capital letter (Circle, Rectangle).
April 04, 2010, at 10:27 PM MST by 71.196.160.210 -
Added lines 1-101:

(:source lang=python:)

""" Classes and objects http://openbookproject.net/thinkcs/python/english2e/ch13.html """

  1. Let's write a class that represent the mathematical notion of
  2. a point in two dimensions.
  3. A point is associated with two coordinates, and let's call them
  4. x and y.
  5. We could just store the two values in a list or a tuple.
  6. But when we put them in an object we can also give the object
  7. methods that are appropriate for a two dimensional point.
  8. Here's the simplest way to define a class:

class Point :

    pass
  1. Let's create an instance of the class:

p = Point()

  1. we just called the constructor of the Point class.
  2. Attributes
  3. Attributes are the data that are associated with an object
  4. We can add/access attributes using the dot notation:

p.x = 3 p.y = 4

  1. We can now do things with objects of this class:

def distance_to_origin(p) :

    """compute the distance of a point from the origin"""
    import math
    return math.sqrt(p.x*p.x + p.y *p.y)
  1. now we can do:

print distance_to_origin(p)

  1. Issues:
  2. we want to be able to do p.distance() as opposed to calling it
  3. as a regular function, i.e. we want distance to be a method of Point.
  4. How do we make sure that a point has x and y attributes?
  5. Let's redefine the Point class:

import math class Point :

    def __init__(self, x, y) :
        self.x = x
        self.y = y
    def distance_to_origin(self) :
        """compute the distance of a point from the origin"""
        return math.sqrt(self.x*self.x + self.y *self.y)
    def distance(self, other) :
        """compute the distance between a point object and another
        point object"""
        return math.sqrt((self.x - other.x)**2 + 
                         (self.y - other.y)**2)

p1 = Point(1, 2) p2 = Point(4, 6) print p1.distance(p2)

  1. let's print the objects:

print p1 print p2

  1. the result is not very informative.
  2. let's fix that using the __str__ method:

import math class Point :

    """Point is a class that stores a two dimensional point"""
    def __init__(self, x=0, y=0) :
        self.x = x
        self.y = y
    def distance_to_origin(self) :
        """compute the distance of a point from the origin"""
        return math.sqrt(self.x*self.x + self.y *self.y)
    def distance(self, other) :
        """compute the distance between a point object and another
        point object"""
        return math.sqrt((self.x - other.x)**2 + 
                         (self.y - other.y)**2)
    def __str__(self) :
        return "Point object with x = d" % (self.x, self.y)

p1 = Point(1, 2) p2 = Point(4, 6)

  1. printing the ojbects produces a more informative result:

print p1 print p2

  1. As a rule, whenever you write a classe you should also write
  2. an __str__ method - it's very useful for debugging!

(:sourceend:)