From the course: Programming Foundations: Beyond the Fundamentals

Introduction to object-oriented programming - Python Tutorial

From the course: Programming Foundations: Beyond the Fundamentals

Introduction to object-oriented programming

- I enjoy fixing things around my house. Over the course of a number of projects I've accumulated quite a few tools. Now I store those all in a big tool drawer that rolls and that's great, but it doesn't really make sense to move the whole thing around my house for different projects. And unless I keep those drawers really well organized it can be a challenge to locate the specific tool I'm looking for. Turns out though, that when I need one tool there are a few others that I often use with it, as well as a lot that I never need at the same time. To make my tool collection more flexible I've organized some tools into a couple smaller tool bags. My plumbing tool bag contains things like an adjustable wrench and Teflon tape. And my bag for electrical work. Well, that's where I keep my volt meter, my wire cutters, things like that. Now, when I need to make a repair or start a project I have all the necessary tools together in a smaller tool bag, which is easier to lift and carry. It's also a little easier for me to find tools inside the bags. As your programs get larger and more complex having a structure for organizing your code becomes increasingly important. Different programming languages offer different tools for code organization and some offer more than one. And there are a number of different approaches or paradigms for organizing code. One of the most common approaches to structuring code in modern programming languages is known as object oriented programming or OOP. Object-oriented code breaks a program up into smaller parts known as objects. Each of these objects has its own distinct focus. Objects communicate with each other to make the program function but the division into smaller units makes the code easier to maintain and easier to reuse. In object-oriented code each object has attributes and behaviors. Each attribute is data that the object has and each behavior is something that the object can do. These attributes are referred to as properties and the behaviors are called methods. I can use an object's methods to work with its properties and I can do this anywhere in my program. In an object oriented language you create objects using a blueprint known as a class. A class describes the types of attributes and behaviors that an object should have. You can use the same class to build out multiple objects based on the same pattern but containing different property values. For instance, a to-do list app might contain a class that creates list objects. Your code might then use the class to create a home to-do list, and a work to-do list. Each would be set up to contain the same types of information, such as tasks, due dates, and priorities. And each would have the same methods to do things like adding items to the list, marking items as completed, or editing items. But the actual data would be different for each object. Your code could even customize objects based on the same class. For instance perhaps the work to-do list object would add support for an additional method like marking a list item as billable. Although a Python program is not required to use classes and objects, using these features can make complex code easier to work with and maintain, which is something we all strive for as developers.

Contents