Object-Oriented Programming in Python

python logo

✅ Objectives

  • Define Object-Oriented Programming
  • Understand the benefits of OOP
  • Build Classes
  • Create instances of those classes
  • Use __init__ to instantiate objects with attribute values
  • Add instance methods to our classes
  • Understand the use of the `self`` keyword in instances
  • Know the principles of OO Design
  • Stretch: object properties, mass assignment

What is OOP? 🤔

  • a programming paradigm
  • seeks to encapsulate information and it’s related behaviors together as objects
  • models concepts and objects in the real world.
  • easier to reason about and solve problems involving those data,
  • facilitates structuring our programs in ways that can share and reuse these objects.
  • contrast to Procedural Programming,
    • written in sequential order and
    • procedures are called when behaviour needs to be shared between pages in an application.

Classes and Instances

A Python Class is…

  • a blueprint or template for creating individual objects
  • a data structure which assigns values and methods to objects

An object is…

  • an individual collection of variables (attributes), functions (methods), and data structures
  • constructed from a class
  • also called an 'instance'
  • a representation of a real world object or event

You actually already have some experience with classes and instances!

  
    type("hello") # => < class 'str'>
    42.__class__ # => < class 'int'>
  

What happens when you enter dir("world") in a Python shell?

Let’s build a class and some instances! 👷

Some strengths of OOP

  • having total control of what objects look like just by updating their class

Some weaknesses of OOP

  • becuase my objects have to conform to a class, I lose flexibility in changing their attributes without changing the class

Example Application Domains

  • healthcare
  • FinTech/banking
  • insurance
  • sales
  • eCommerce
  • accounting
  • booking software for hospitality and travel

OOP Design Principles 🧭

  • single responsibility
  • separation of concerns
  • DRY
  • domain modeling

Single Responsibility

Overloaded 🚫
Classes separated to reduce complexity of our classes 👍

Separation of Concerns

  • Supports high cohesion among components
  • Supports low coupling among components
  • Increases modularity
  • Increases maintainability
  • Increases reusability
Cohesion
  • cohesive components perform only one task
  • cohesion is the internal glue that keeps a module together
  • it is a measure fo the degree to which the elements in the module are functionally related
Coupling
  • good software has low coupling
  • coupling increases with the number of calls or the amount of data shared between modules
  • a design with high coupling will have more errors
  • it measures the degree of interdependence between modules
D.R.Y. 🌞🌵

Domain Modeling 📐

A structured visual representation of interconnected concepts or real-world objects that incorporates vocabulary, key concepts, behavior, and relationships of all its entities.

How will objects help us going forward? 🚗