Loading video player…

Guiding Principles of OOP

00:00 Like I mentioned, OOP is a programming paradigm and it’s used in a wide variety of languages. At a basic level, it’s all about structuring your code in a way that data or properties are grouped together with related behaviors or actions.

00:13 This is done via objects and classes. Objects contain both data and behaviors called attributes and methods. Attributes represent data and methods represent behaviors.

00:26 You can use them to emulate real-world entities or systems. For example, you could have an object that represents a person with properties like name and age and behaviors like walk, talk, or write code, but where do objects come from?

00:39 That’s where classes come in. Classes provide a blueprint for creating objects. These objects are then called instances of that class. Classes also define the type of instances created from the class, so if you had a Person object, they would be based on a Person class.

00:57 The class is what defines the properties the object can have and what actions it can take, and because the class is a blueprint, you could use that class to create as many instances of Person as you want.

01:08 8 billion, if you like. As a programming paradigm or philosophy, OOP has four guiding principles: encapsulation, inheritance, abstraction, and polymorphism.

01:21 Starting with encapsulation. Encapsulation allows you to group data and behaviors into a single unit, an object. The object has the data it needs, and that data should only be modified by methods exposed by the object, which would be part of its behavior.

01:36 This promotes data integrity and modularity in the application. Inheritance is a means of code reuse. You can create new classes that are based on existing classes and form class hierarchies.

01:47 Child classes inherit the methods and attributes of their parents and can even add to or modify them. This is great for code reuse and reducing code duplication.

01:59 Abstraction is the practice of hiding implementation details, exposing only the essential functionality of an object, and thereby simplifying interactions. Broadly, you shouldn’t need to know how an object does something to use it, only what it does.

02:15 Finally, polymorphism allows you to treat objects of different types as if they’re all instances of the same base type, as long as they have a common interface.

02:26 That is, if they behave the same way under the same circumstances.

02:30 This jives well with Python’s approach to typing in general, known as duck typing. If it looks like a duck and quacks like a duck, treat it like a duck. As the developer, this lets you code to interfaces and not types.

02:44 That’s a lot of theory, but what does OOP look like in practice? In Python, consider this example. Say you have a database of people, like officers on a ship’s crew, with information about each crew member.

02:56 How would you store this data for use in your program?

02:59 One approach would be to use one of Python’s built-in primitive data structures, like the list. It might look like this: kirk = [ with the values "James Kirk", 34, "captain", 2265].

03:12 This is a very bare-bones approach. If you want to access a specific value like Kirk’s rank, you’re limited to using indexes, grabbing the third element in the list by accessing index two.

03:24 This requires remembering the correct index and ensuring that every list of crew member details is the same length and has the same elements in the same order.

03:32 That’s all extra work for you, the developer, and that’s what makes this error-prone, hard to maintain, and difficult to scale.

03:41 Alternatively, using OOP and a little more upfront programming effort, you could create a Crew class. To keep things simple, this example doesn’t show how it’s made.

03:50 It just assumes you’ve already made it. Then instead of creating kirk, the list, you would create kirk, the instance of the Crew class, passing in the same values.

04:00 With this approach, grabbing his rank would be as simple as accessing the .rank attribute using the dot accessor using classes like this. Any object holding data about crew members will have a consistent interface, with the added benefit of accessing attributes by name, simplifying things for whoever is writing and later reading the code.

04:20 Additionally, because you create the class upfront, you can later add or remove functionality by modifying the class definition, i.e., the blueprint. That’s what makes the OOP approach extendable as well, like adding behaviors via methods, something you wouldn’t be able to do with the list approach.

04:37 Okay, hopefully I’ve sold you now on the benefits of OOP and you’re ready to ask the question, how do I create classes? Well, the answer is waiting for you in the next lesson.

Become a Member to join the conversation.