Welcome to AR-327 !!


Today's learning objectives:

  • Understand the fundamentals of object-oriented programming
  • Get familiar with classes
  • Get confident in creating classes and objects

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP)

When programming, we often need to represent concepts that cannot be represented with a single integer, a string, or even a dictionnary. We need something that can represent those concepts in a structured and repeatable way

Object-Oriented Programming (OOP)

Example:

You need to represent a door in code. You have different heights, widths, depths, materials, handle heights, pivoting sides, ... but they are all doors

Object-Oriented Programming (OOP)

all doors

You don't want to write custom code for every door
(remember, last course we saw function, which follow the same logic: every time you have a similar opration to perform: create a function)
⬇️
Hence the usage of classes

But what is a classe?

a Class is a code template to create objects. Objects have member attributes and have methods associated with them.

But what is a classe?

a Class is a code template to create objects. Objects have member attributes and have methods associated with them.

Let's take a look at an example !

Let's create a class for a timber beam

There are many types of timber beams possible:
Beam Type 1
Solid timber beam
Beam Type 2
Duo beam
Beam Type 3
Gluelam beam

Let's create a class for a timber beam

There are many types of timber beams possible:
Beam Type 1
Solid timber beam
Beam Type 2
Duo beam
Beam Type 3
Gluelam beam


What defines a beam ? Let's discuss it together, and decide how we eill define our class

Let's create a class for a timber beam

Our class should contain:

  • The length of the beam
  • The width of the beam
  • The height of the beam
  • The material of the beam


These are called attributes

Let's create a class for a timber beam

Our class should contain:

  • The length of the beam -> float
  • The width of the beam -> float
  • The height of the beam -> float
  • The material of the beam -> string (for ex.)


These are called attributes

Let's create a class for a timber beam

	    
            class Beam:
                # This is the constructor
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    # These are the attributes:
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material    
        
    

Let's create a class for a timber beam

	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material    
        
    
What is `self` ?

Let's create a class for a timber beam

	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material    
        
    
`self` allows the function or constructor to access the class' attibutes and methods. It baysically says "hey, this method/constructor will need to access the internal data of the class "

Let's create and instanciate a class for a timber beam

	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material    
            
            my_super_beam = Beam(3000, 80, 160, "duo")
            print(my_super_beam.height) # What will appear in the terminal ?
        
    

Let's create a class for a timber beam

But we can also embed functions in classes, called methods

Let's create a class for a timber beam

For example we can embed a function that calculates the volume of the beam:
	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material
                
                # This is a method
                def calculate_volume(self):
                    return(self.length * self.width * self.height)
            
            my_super_beam = Beam(3000, 80, 160, "duo")
            print(my_super_beam.calculate_volume()) # What will appear in the terminal ?
        
    

Let's instanciate multiple objects

	    
            beam_a = Beam(4200, 120, 200, "gluelam")
            beam_b = Beam(3600, 120, 160, "duo")
            beam_c = Beam(3600, 120, 200, "gluelam")
            if beam_a.calculate_volume() > beam_b.calculate_volume():
                print("beam_a is bigger than beam_b")
            else:
                print("beam_b is bigger or as big as beam_a")
        
    
Now we have created three beams objects that all share the same attributes and methods

Small exercice on classes (20 min)

In the website, go to 04) Object-oriented programming and solve the first exercice

Class attributes

Class attributes


As seen in the previous section, attributes are variables stored "inside" a class.

Class attributes

As seen in the previous section, attributes are variables stored "inside" a class.
In our beam example, the length, width, height and material are attributes of the class `Beam`
	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material    
        
    

Class attributes

The attributes can be accessed and set like this:
	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material 
            
            my_super_beam = Beam(4200, 120, 200, "gluelam")

            my_super_beam.length = 2200 # Retrieveing and changing the length from 4200 to 2200
        
    

Class methods

Class methods


A method is a function stored "inside" a class

Class methods


A method is a function stored "inside" a class
	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material
                
                def calculate_volume(self):
                    return(self.length * self.width * self.height)
                    
        
    

Class methods


A method is a function stored "inside" a class Much like a function, you call a method
	    
            class Beam:
                def __init__(self, length: float, 
                                   width: float, 
                                   height: float,
                                   material: str):
                    self.length = length
                    self.width = width
                    self.height = height
                    self.material = material
                
                def calculate_volume(self):
                    return(self.length * self.width * self.height)

            my_super_beam = Beam(3000, 80, 160, "duo")
            volume = my_super_beam.calculate_volume() # here we call the method
        
    

Small exercice on class methods (10 min)


In the website, go to 04) oop-methods and solve the first exercice

Assignment on Python OOP: