Main.Shapes History
Hide minor edits - Show changes to markup
rect3.set_width(5)
rect3.width = 3
- Getters and setters
- 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
- 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
return Rectangle(self.width, self.height)
return Circle(self.radius)
return self.width * self.height
return math.pi * self.radius * self.radius
return 2 * self.width + 2 * self.height
return 2 * math.pi * self.radius
self.height *= s
self.width *= s
self.radius *= s
return "Rectangle with width = f" % (self.width, self.height)
- This version includes "getter" and "setter" methods for accessing and modifying
- the object's data.
- Why the get and set methods? Afterall, the user can set those by himself
- without having to use these methods:
- You can easily set rectangle height by: rect.height = 5
- In java there is good reason to use such methods but not in python.
- See for example: http://tomayko.com/writings/getters-setters-fuxors
- 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
return "Circle with radius = self.radius
Classes and objects http://openbookproject.net/thinkcs/python/english2e/ch13.html
More object oriented programming. http://openbookproject.net/thinkcs/python/english2e/ch15.html see also http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
- Let's write a class that represent the mathematical notion of
- a point in two dimensions.
- A point is associated with two coordinates, and let's call them
- x and y.
- We could just store the two values in a list or a tuple.
- But when we put them in an object we can also give the object
- methods that are appropriate for a two dimensional point.
- Here's the simplest way to define a class:
class Point :
pass
- Let's create an instance of the class:
p = Point()
- we just called the constructor of the Point class.
- Attributes
- Attributes are the data that are associated with an object
- We can add/access attributes using the dot notation:
p.x = 3 p.y = 4
- 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)
- now we can do:
print distance_to_origin(p)
- Issues:
- we want to be able to do p.distance() as opposed to calling it
- as a regular function, i.e. we want distance to be a method of Point.
- How do we make sure that a point has x and y attributes?
- Let's redefine the Point 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 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
- Let's modify rect3 now:
rect3.set_width(5) print rect3 print rect1
- Modifying rect3 has changed rect1! That is because they both
- point to the same Rectangle object.
- Getters and setters
- 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)
- This version includes "getter" and "setter" methods for accessing and modifying
- the object's data.
- Why the get and set methods? Afterall, the user can set those by himself
- without having to use these methods:
- You can easily set rectangle height by: rect.height = 5
- In java there is good reason to use such methods but not in python.
- See for example: http://tomayko.com/writings/getters-setters-fuxors
- Let's define another type of shape:
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)
- let's print the objects:
print p1 print p2
- the result is not very informative.
- 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)
- printing the ojbects produces a more informative result:
print p1 print p2
- As a rule, whenever you write a classe you should also write
- an __str__ method - it's very useful for debugging!
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()
- Note that both classes present essentially the same interface, i.e.
- the same set of methods.
- 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()
- Naming convention: Class names begin with a capital letter (Circle, Rectangle).
(:source lang=python:)
""" Classes and objects http://openbookproject.net/thinkcs/python/english2e/ch13.html """
- Let's write a class that represent the mathematical notion of
- a point in two dimensions.
- A point is associated with two coordinates, and let's call them
- x and y.
- We could just store the two values in a list or a tuple.
- But when we put them in an object we can also give the object
- methods that are appropriate for a two dimensional point.
- Here's the simplest way to define a class:
class Point :
pass
- Let's create an instance of the class:
p = Point()
- we just called the constructor of the Point class.
- Attributes
- Attributes are the data that are associated with an object
- We can add/access attributes using the dot notation:
p.x = 3 p.y = 4
- 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)
- now we can do:
print distance_to_origin(p)
- Issues:
- we want to be able to do p.distance() as opposed to calling it
- as a regular function, i.e. we want distance to be a method of Point.
- How do we make sure that a point has x and y attributes?
- 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)
- let's print the objects:
print p1 print p2
- the result is not very informative.
- 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)
- printing the ojbects produces a more informative result:
print p1 print p2
- As a rule, whenever you write a classe you should also write
- an __str__ method - it's very useful for debugging!
(:sourceend:)
