- learn advanced python class concepts
- use default parameters with python classes
- use static class variables
- TODO: add
@staticmethod
Python allows us to provide default values for parameters in any function we provide. Let's write a Point
class that has x
and y
variables. If no x
and y
values are provided when a Point
is initialized x
and y
should both default to zero.
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
Add a __str__
method that prints points in the standard tuple format (x, y)
:
def __str__(self):
return '({},{})'.format(self.x, self.y)
Add a method called distance
that calculates and returns the distance between the current point and the origin. Use the mathematical distance formula where the distance between a point and the origin is defined as the square root of (x_x + y_y).
Use Python's math module by adding import math
at the top:
import math
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance(self):
return math.sqrt(self.x**2+self.y**2)
OR leverage the fact that the square root of a number is equivalent to raising that number to the 1/2
def distance(self):
return (self.x ** 2 + self.y ** 2) ** .5
Test it:
p0 = Point()
p2 = Point(3, 4)
print(p0.distance())
0.0
print(p2.distance())
5.0
Let's say we wanted to make our distance method a little more robust. Let's change our distance method so instead of just returning the distance between a point and the origin, it can give us the distance between the point it's invoked on, and any other point of our choosing.
Change the distance
method to accept a reference to a second Point as an optional parameter. The second point parameter should have a default value of None
. The distance formula between two points is the square root of the difference between the two x coordinates (dx) squared, plus the difference between the two y coordinates (dy): sqrt(dx^2 + dy^2)
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# def distance(self):
# return math.sqrt(self.x**2+self.y**2)
def distance(self, p2):
dx = self.x - p2.x
dy = self.y - p2.y
return math.sqrt(dx**2 + dy**2)
# p0 = Point()
# p2 = Point(3, 4)
p3 = Point(6,13)
p4 = Point(1,1)
# print(p0.distance())
# print(p2.distance())
print(p3.distance(p4))
Great! Now, what if we wanted to make this method do both? i.e. we want to have the option of passing in a second point, but we also want the method to give us the distance from the origin if we dont pass in a second point. We can do this by defaulting the p2
parameter to None
, then writing a conditional that sets p2
to the origin if a second point isn't passed in.
But first, let's create a class variable called ORIGIN
which is a Point
object at (0,0)
and is accessible by ALL instances of Point
. Class variables are available even without creating any instances of a class. In this case, we're creating a class variable that holds an instance of the class itself, so we have to attach it to the class after we've defined the class.
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# def distance(self):
# return math.sqrt(self.x**2+self.y**2)
def distance(self, p2):
dx = self.x - p2.x
dy = self.y - p2.y
return math.sqrt(dx**2 + dy**2)
# attach ORIGIN after the Point class is defined
Point.ORIGIN = Point()
# p0 = Point()
# p2 = Point(3, 4)
p3 = Point(6,13)
p4 = Point(1,1)
# print(p0.distance())
# print(p2.distance())
print(p3.distance(p4))
# we can access ORIGIN through the Point class.
print(Point.ORIGIN)
(0,0)
p4 = Point(3,4)
p5 = Point(3,19)
# Distance defaults to calculating how far away a Point is from ORIGIN
p4.distance()
5.0
# Distance will calculate the distance from one point to another if a
# a second Point is provided as a parameter.
p4.distance(p5)
15.0
TODO: add this