11. Python Classes - a brief introduction#

Python provides some common, versatile, general purpose data structures. Additional data structures can be found in various Python packages, such as Pandas, numpy, and collections.

New data structures can be built from these general purpose data structures by combining them in different ways and adding new methods. One way to do this would be to create a dictionary whose keys are used to access the different parts and methods of the new data structure.

11.1. New data structures from old#

11.1.1. Example -1 A point data structure - take 1#

def point_add(a,b) :
    return {"x" : a["x"] + b["x"],"y" : a["y"] + b["y"]}
    
a = {"x" : 3.14,"y" : 2.71}
b = {"x" : -1, "y" : 1}
c = point_add(a,b)
print(c["x"], " : ",c["y"])
2.14  :  3.71
type(c)
dict

However, Python provides a specific means of combining other Python objects together.

11.1.2. Example - 2 A point data structure - take 2#

class point :
    x = None
    y = None
p_1 = point()
type(p_1)
__main__.point
p_1.x
p_1.y
p_1.x = 3
p_1.y = 4
p_1.x
3
p_2.y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [12], line 1
----> 1 p_2.y

NameError: name 'p_2' is not defined

11.1.3. Exercise - 1#

Discuss the differences between Examples 1 and 2

A Python class can also contain functions. Of course it can - functions are data !!

11.2. Member functions#

11.2.1. Example 3#

class point :
    x = None
    y = None
    def add(a,b) :
        c = point()
        c.x = a.x + b.x
        c.y = a.y = b.y
        return c
p_1 = point()
p_1.x = 1
p_1.y = 2
p_2 = point()
p_2.x = 3
p_2.y = 4
p_3 = point.add(p_1,p_2) 
p_3.x
4
p_2.x
3

A Python class also has some special functions which you can overide. These start and and with a doulbe underscore.

11.2.2. Example 4 - A constructor#

class point :
    x = None
    y = None
    def add(a,b) :
        c = point()
        c.x = a.x + b.x
        c.y = a.y = b.y
        return c
    def __init__(self,x = 0.0,y = 0.0) :
        self.x = x
        self.y = y
p_4 = point(5,6)
p_4.x
5

11.2.3. Example 5 - Print#

class point :
    x = None
    y = None
    def add(a,b) :
        c = point()
        c.x = a.x + b.x
        c.y = a.y = b.y
        return c
    def __init__(self,x = 0.0,y = 0.0) :
        self.x = x
        self.y = y
    def print(a) :
        print("(",a.x,",",a.y,")")
p_5 = point(1,2)
point.print(p_5)
( 1 , 2 )

11.2.4. Exercise 2#

What happens if you use point.print to print something other than a point ?

point.print("hello")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In [46], line 1
----> 1 point.print("hello")

Cell In [44], line 13, in point.print(a)
     12 def print(a) :
---> 13     print("(",a.x,",",a.y,")")

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

11.2.5. Example 6 - Print (take 2)#

class point :
    x = None
    y = None
    def add(a,b) :
        c = point()
        c.x = a.x + b.x
        c.y = a.y = b.y
        return  c
    def __init__(self,x = 0.0,y = 0.0) :
        self.x = x
        self.y = y
        
    def print(self) :
        print("(",self.x,",",self.y,")")
p_6 = point(7,-5)
p_6.print()
( 7 , -5 )

11.2.6. Exercise 3#

What is self ?

11.2.7. Exercise 4#

Compare and contrast the two versions of print in Examples 5 and 6

11.2.8. Exerise 5#

Implement a function to determine the Euclidean distance between two points.

11.2.9. Exerise 6#

Implement a member function to determine the Euclidean distance between two points.

11.2.10. Exercise 7#

Implement a class to represent a straight line (in the plane).

11.2.11. Exercise 8#

Other than any member functions, what are the differences between the point type and your line type ?

11.2.12. Exercise 9#

Other than any member functions, what things are common to the point type and your line type ?

11.2.13. Exercise 10#

Implement a class to represent a line segment (in the plane).

11.2.14. Exercise 11#

Write a function, or a member function, (or both), which determines the angle between two line segments.