Main.ClassesIntroduction History

Hide minor edits - Show changes to markup

March 28, 2010, at 09:56 PM MST by 71.196.160.210 -
Added lines 1-100:

(: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 :

    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)
    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:)