class className: [statement-1] . . [statement-N]
class Point:
pass
a = Point()
a.x = 0
a.y = 0
print(a.x, a.y)
# OK but NOT best practice!
0 0
class Point:
"""
Point class to represent and manipulate x, y coords
"""
count = 0 # class variable/attribute
# constructor to customize the initial state of an object
# first argument refers to the instance being manipulated;
# it is customary to name this parameter self; but can be anything
def __init__(self, xx=0, yy=0):
"""Create a new point with given x and y coords"""
# x and y are object variables/attributes
self.x = xx
self.y = yy
Point.count += 1 # increment class variable
def __del__(self):
Point.count -= 1
# instantiate an object
p = Point()
# what is the access specifier for attributes?
print(p.x, p.y)
print(Point.count) # access class variable outside class
p1 = Point(2, 3)
print(p1.x, p1.y)
print(Point.count)
# Run this cell few times and see the value of Point.count
# How do you fix this problem? Use __del__ destructor method.
0 0 1 2 3 2
from IPython.display import IFrame
src = """http://pythontutor.com/iframe-embed.html#code=class%20Point%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20Point%20class%20represents%20and%20manipulates%20x,y%20coords%0A%20%20%20%20%22%22%22%0A%20%20%20%20count%20%3D%200%20%23%20class%20variable/attribute%0A%20%20%20%20%0A%20%20%20%20%23%20constructor%20to%20customize%20the%20initial%20state%20of%20an%20object%0A%20%20%20%20%23%20first%20argument%20refers%20to%20the%20instance%20being%20manipulated%3B%0A%20%20%20%20%23%20it%20is%20customary%20to%20name%20this%20parameter%20self%3B%20but%20can%20be%20anything%0A%20%20%20%20def%20__init__%28self,%20xx%3D0,%20yy%3D0%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22Create%20a%20new%20point%20with%20given%20x%20and%20y%20coords%22%22%22%0A%20%20%20%20%20%20%20%20%23%20x%20and%20y%20are%20object%20variables/attributes%0A%20%20%20%20%20%20%20%20self.x%20%3D%20xx%0A%20%20%20%20%20%20%20%20self.y%20%3D%20yy%0A%20%20%20%20%20%20%20%20Point.count%20%2B%3D%201%20%23%20increment%20class%20variable%0A%20%20%20%20%20%20%20%20%0Ap%20%3D%20Point%28%29%0Aprint%28p.x,%20p.y%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"""
IFrame(src, width=900, height=600)
class Point:
"""
Point class represents and manipulates x,y coords
"""
count = 0
def __init__(self, xx=0, yy=0):
"""Create a new point with given x and y coords"""
self.x = xx
self.y = yy
Point.count += 1
def dist_from_origin(self):
import math
dist = math.sqrt(self.x**2+self.y**2)
return dist
def __str__(self):
return "({}, {})".format(self.x, self.y)
p1 = Point(2, 2)
print(p1.dist_from_origin())
2.8284271247461903
p2 = Point(3, 2)
print(p2)
p2.x = 4
p2.y = 10
print(p2)
(3, 2) (4, 10)
class Point:
"""
Point class represents and manipulates x,y coords
"""
count = 0
def __init__(self, xx=0, yy=0):
"""Create a new point with given x and y coords"""
self.x = xx
self.y = yy
Point.count += 1
def dist_from_origin(self):
import math
dist = math.sqrt(self.x**2+self.y**2)
return dist
def __str__(self):
return "({}, {})".format(self.x, self.y)
def move(self, xx, yy):
self.x = xx
self.y = yy
p3 = Point()
print(p3)
p3.move(10, 20)
print(p3)
(0, 0) (10, 20)
import copy
p2 = Point(3, 4)
p3 = p2 # alias or deepcopy?
print(p2 is p3) # checks if two references refer to the same object
p4 = copy.deepcopy(p2)
print(p2 is p4)
True False
def print_point(pt):
#pt.x = 100
#pt.y = 100
print('({0}, {1})'.format(pt.x, pt.y))
p = Point(10, 10)
print_point(p)
#print(p)
print(p.x, p.y)
(10, 10) 10 10
def midpoint(p1, p2):
"""Returns the midpoint of points p1 and p2"""
mx = (p1.x + p2.x)/2
my = (p2.x + p2.y)/2
return Point(mx, my)
p = Point(4, 6)
q = Point(6, 4)
r = midpoint(p, q)
print_point(r) # fix this
print(r)
(5.0, 5.0) (5.0, 5.0)
exercise 1: design a class to represent a triangle and implement methods to calculate area and perimeter.
class Rectangle:
""" A class to manufacture rectangle objects """
def __init__(self, posn, w, h):
""" Initialize rectangle at posn, with width w, height h """
self.corner = posn
self.width = w
self.height = h
def __str__(self):
return "({0}, {1}, {2})".format(self.corner, self.width, self.height)
box = Rectangle(Point(0, 0), 100, 200)
bomb = Rectangle(Point(100, 80), 5, 10) # In my video game
print("box: ", box)
print("bomb: ", bomb)
box: ((0, 0), 100, 200) bomb: ((100, 80), 5, 10)
r1 = Rectangle(Point(1, 1), 10, 5)
r2 = copy.copy(r1)
# r1 is not r2 but two corners are same
r1 is r2
False
r1.corner is r2.corner
True
r1.corner.move(10, 10)
# both r1 and r2 moved
print(r1)
print(r2)
((10, 10), 10, 5) ((10, 10), 10, 5)
r3 = copy.deepcopy(r1)
r1 is r3
False
print(r1, r3)
((10, 10), 10, 5) ((10, 10), 10, 5)
r1.corner.move(20, 20)
# r1 is moved but not r3
print(r1, r3)
((20, 20), 10, 5) ((10, 10), 10, 5)