Classes

  • Classes are the way to use object oriented programming (OOP)
  • Rather than have simple variables, sometimes we would like to group related things and their functionality together.
  • Think in terms of objects, like a person.
    • Objects have attributes; a person has a name, age, weight, hair color, race, etc.
    • Objects can perform actions using functions; a person can use their attributes to calculate other properties, or sing, or run, or move, etc.

Excercise

  • Think of something in this course, or in engineering or math that we could think of as an object with attributes and functions.

Example: define a cube class

In [22]:
class cube():

    def set_side(self, side):
        self.L = side

    def volume(self):
        return self.L**3

    def area(self):
        return 6 * self.L**2
    

Create cube class objects

In [23]:
c1 = cube()         # create a cube object
c2 = cube()         # create another cube object

c1.set_side(2)         # set side length        (ignore self)
c2.set_side(3)         # set side length        (ignore self)

print('Sides   =', c1.L,         c2.L)
print('Areas   =', c1.area(),    c2.area())
print('Volumes =', c1.volume(),  c2.volume())
Sides   = 2 3
Areas   = 24 54
Volumes = 8 27

Excercise

  • Observe the definition and use of this class.
  • List the parts.
  • What makes sense, what is new?

Notes

  • class keyword.
  • End in :
  • The class contains member functions:
    • set_side
    • volume
    • area
  • We have set a data member:
    • L
  • The class definition creates a sort of template for creating individual objects of the class.
    • The definition doesn't make an actual object. That is done later.
  • We then create instances of the class in variables c1 and c2.
    • Note the syntax: c1 = cube().
    • c1 and c2 have their own data members, but the functions work the same.
  • Data members and member functions are accessed with the dot notation: as in c1.L

self

  • In the definition of the class, note the use of the self function argument.
  • self refers to the object itself. That is, the particular instance of the class (c1 or c2).
  • self is the first argument for class member functions.
  • When we define the class member function set_side, we had
    def set_side(self, side):
      self.L = side
    
    When we call that function we had
    c1.set_side(2)
    
  • We ignore self when calling.
    • It is automatically passed as the first argument when we issue the call.

Illustration

  • Normally we call member functions with the dot notation: c1.area() and self is automatically passed in.
  • But we can also call the function using cube.area(c1) and explicitly pass in the object.
In [34]:
Aa = c1.area()       
Ab = cube.area(c1)

print(Aa, Ab)
150 150

Initialization

  • We can include the special function __init__ that is called when class objects are created.
    • (double underscores)
  • This can let us initialize data members without an explicit call.

Redo the cube class with __init__

In [9]:
class cube():
    
    def __init__(self, side_length):
        self.L = side_length
        
    def area(self):
        return 6*self.L**2
    
    def volume(self):
        return self.L**3
In [8]:
c1 = cube(2)          # __init__ is called when we create c1
c2 = cube(3)          # __init__ is called when we create c2

print('Sides   =', c1.L,         c2.L)
print('Areas   =', c1.area(),    c2.area())
print('Volumes =', c1.volume(),  c2.volume())
Sides   = 2 3
Areas   = 24 54
Volumes = 8 27

Exercise

  • Create a cylinder class.
  • What data members (attributes/variables) should it have?
  • What member functions should it have?
In [ ]: