Module 0.11.0 - Object Oriented Programming

  • a programming style based on the use of classes and objects

  • a class is a blueprint for an object

  • an object is a variable created from a class's definition

  • object's have attributes and behaviors

    • an attribute is an object's properties

    • a behavior [or method] is what an object does

0.11.1 - Class's Put Into Context

This will be our hypothetical for most of this lesson

  • let's say you are making a game with python

  • in this game, there will be different types of dogs

  • instead of individually programming each dog, we can create a blueprint or class that all the dogs can follow


  • let's say we make a dog class (or blueprint)

  • we will need to account for a dog's attributes and behaviors

  • the attributes of a dog:

    • name

    • color

    • breed

  • the behaviors of a dog

    • bark

    • they play fetch

    • move forward, backward, left, and right

0.11.2 - Creating A Class

  • "class" statement, creates a class

    • it is a good programming practice to make class's start with a capital letter

      • to differentiate them from functions and modules

  • we now need to add our instance variables, these variables will contain the dogs' attributes

    • things like the dogs name and fur color

  • "__init__" method, initializes instance variables to the class [init = initialize]

    • this "__init__" method is reffered to as the class constructor

  • class methods are structured like functions

  • all methods in a class take "self" as their first parameter

    • "self" parameter, an access point to the current instance of the class

    • the "self" parameter doesn't actually have to be called self, it's just good practice

  • we can now add our dog objects

  • we can output and use the dog object's attributes

0.11.3 - Adding Behaviors

  • now that the dog objects have their own attributes, we can give them behaviors like barking or growling

  • class's can also have attributes that are only accessible to the class

  • created by initializing variables in the class

0.11.4 - Inheritance

  • congrats, the game is sucessful upon it's release

  • eventually, people get tired of dogs, they want more animals like cats

  • now we could make another entire class for cats, but cat's have the same attributes as dogs, but different behaviours

  • for situations like this we can inherit attribues and behaviors from other class's.


  • a super class is a class that other class's [subclass's] can inherit from

  • let's make a super class called "Animal"

let's say that all of the animals in our game share these same attributes

  • name

  • color

  • breed

  • note that if an object doesn't have attributes or behaviours, you don't need to add them in the blueprint or class

  • let's make our animal subclass's

  • to inherit from a class, mention the super class's name in parenthesis "()" after the supclass

now we can make our animal objects

when a subclass inherits from a super class, behaviors or attributes with the same name are overwritten by the subclass

0.11.5 - More Advanced Topics

  • OOP [Object Oriented Programming] can get very complicated very quickly

  • due to this being a beginner friendly, cybersecurity focused, curriculem we will end the topic of OOP here

  • however, CosmodiumCS will be releasing an Advanced level Python course in the near future

    • here we will more advanced topics like generators, recursion, decorators, magic methods [dunders], hiding class data, class and static methods, properties, and more

  • until then, I will link some free and open source resources for if you wish to continue learning