Point Class

In [44]:
import math

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return "({}, {})".format(self.x, self.y)

    def to_polar(self):
        r = math.sqrt( self.x**2 + self.y**2 )
        theta = math.degrees( math.atan( self.y / self.x ) )
        return r, theta

    def distance(self, other):
        return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
In [45]:
p1 = Point(1,3)
In [46]:
print(p1)
(1, 3)
In [47]:
p1
Out[47]:
(1, 3)
In [48]:
p2 = Point(-1,1)
In [49]:
p1.distance(p2)
Out[49]:
2.8284271247461903
In [50]:
p1.x
Out[50]:
1
In [51]:
p1.y
Out[51]:
3

Line Class

In [52]:
class Line(object):
    def __init__(self, p0, p1):
        self.p0 = p0
        self.p1 = p1
        
    def is_vertical(self):
        return self.p0.x == self.p1.x

    def get_slope(self):
        if self.is_vertical():
            return float('inf')
        else:
            return (self.p1.y - self.p0.y) / (self.p1.x - self.p0.x)
        
    def get_y_intercept(self):
        if self.is_vertical():
            if self.p0.x == 0:
                return 0.0
            else:
                return float('NaN')
        else:
            return self.p0.y - self.get_slope()*self.p0.x

    def __repr__(self):
        if self.is_vertical():
            return "x = {}".format(self.p0.x)
        else:
            return "y = {}*x + {}".format(self.get_slope(), self.get_y_intercept())
In [53]:
# y = 5x + 7
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
print(l)
y = 5.0*x + 7.0

Getters/Setters

In [54]:
class Point(object):
    def __init__(self, x, y):
        self.set_x(x)
        self.set_y(y)
    
    def get_x(self):
        return self._x
    
    def set_x(self, x):
        # The following line checks whether 'x' is an int or a float.
        # If it isn't, it raises an "exception", which we'll see in
        # more detail later in the quarter
        if not isinstance(x, (int, float)): raise ValueError("Not a number")
        self._x = x
        
    def get_y(self):
        return self._y
    
    def set_y(self, y):
        if not isinstance(y, (int, float)): raise ValueError("Nor a number")
        self._y = y        
    
    def __repr__(self):
        return "({}, {})".format(self._x, self._y)
    
    def to_polar(self):
        r = math.sqrt( self._x**2 + self._y**2 )
        theta = math.degrees( math.atan( self._y / self._x ) )
        return r, theta    
    
    def distance(self, other):
        return math.sqrt((self._x - other._x)**2 + (self._y - other._y)**2)
In [55]:
p = Point(2,3)
In [56]:
p.get_x()
Out[56]:
2
In [57]:
p.set_x(3.14)
In [58]:
p
Out[58]:
(3.14, 3)
In [59]:
p.set_x("rofl")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-59-5c3ce0517695> in <module>()
----> 1 p.set_x("rofl")

<ipython-input-54-33709e37f4ec> in set_x(self, x)
     11         # If it isn't, it raises an "exception", which we'll see in
     12         # more detail later in the quarter
---> 13         if not isinstance(x, (int, float)): raise ValueError("Not a number")
     14         self._x = x
     15 

ValueError: Not a number
In [60]:
p._x
Out[60]:
3.14
In [61]:
p._x = 5
In [62]:
p
Out[62]:
(5, 3)
In [63]:
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
In [64]:
l.get_slope()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-64-7d89894a0208> in <module>()
----> 1 l.get_slope()

<ipython-input-52-d66e5d1f573a> in get_slope(self)
      8 
      9     def get_slope(self):
---> 10         if self.is_vertical():
     11             return float('inf')
     12         else:

<ipython-input-52-d66e5d1f573a> in is_vertical(self)
      5 
      6     def is_vertical(self):
----> 7         return self.p0.x == self.p1.x
      8 
      9     def get_slope(self):

AttributeError: 'Point' object has no attribute 'x'

Properties

In [65]:
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def get_x(self):
        return self._x
    
    def set_x(self, x):
        if not isinstance(x, (int, float)): raise ValueError("Not a number")
        self._x = x
        
    def get_y(self):
        return self._y
    
    def set_y(self, y):
        if not isinstance(y, (int, float)): raise ValueError("Nor a number")
        self._y = y        

    # Property definitions. Must be done *after* we've defined the getters/setters.
    x = property(get_x, set_x)
    y = property(get_y, set_y)

    def __repr__(self):
        return "({}, {})".format(self.x, self.y)
    
    def to_polar(self):
        r = math.sqrt( self.x**2 + self.y**2 )
        theta = math.degrees( math.atan( self.y / self.x ) )
        return r, theta    
    
    def distance(self, other):
        return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
In [66]:
p = Point(2,3)
In [67]:
p.x
Out[67]:
2
In [68]:
p.y
Out[68]:
3
In [69]:
p.x = 6
In [70]:
p
Out[70]:
(6, 3)
In [71]:
p.x = "foo"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-71-887bd1d5af06> in <module>()
----> 1 p.x = "foo"

<ipython-input-65-f8c118573f4e> in set_x(self, x)
      8 
      9     def set_x(self, x):
---> 10         if not isinstance(x, (int, float)): raise ValueError("Not a number")
     11         self._x = x
     12 

ValueError: Not a number
In [72]:
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
In [73]:
l.get_slope()
Out[73]:
5.0
In [74]:
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    # Notice how the name of the method is not "get_x" but simply "x" 
    # the name of the attribute.
    @property
    def x(self):
        return self._x
    
    # Now we define the setter, which has the same name, but an
    # additional parameter (the value being set). Notice how the
    # decorator isn't "@property", it's the name the attribute and .setter
    @x.setter
    def x(self, x):
        if not isinstance(x, (int, float)): raise ValueError("Not a number")
        self._x = x
        
    @property
    def y(self):
        return self._y
    
    @y.setter
    def y(self, y):
        if not isinstance(y, (int, float)): raise ValueError("Nor a number")
        self._y = y        

    def __repr__(self):
        return "({}, {})".format(self.x, self.y)
    
    def to_polar(self):
        r = math.sqrt( self.x**2 + self.y**2 )
        theta = math.degrees( math.atan( self.y / self.x ) )
        return r, theta    
    
    def distance(self, other):
        return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
In [75]:
p = Point(2,3)
In [76]:
p.x
Out[76]:
2
In [77]:
p.x = 5
In [78]:
p
Out[78]:
(5, 3)
In [79]:
p.x = "foo"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-887bd1d5af06> in <module>()
----> 1 p.x = "foo"

<ipython-input-74-8ddc428de74c> in x(self, x)
     15     @x.setter
     16     def x(self, x):
---> 17         if not isinstance(x, (int, float)): raise ValueError("Not a number")
     18         self._x = x
     19 

ValueError: Not a number