In this lesson, you’ll learn whatobject-oriented programming (OOP) is and why it’s used.
Lots of popular frameworks and libraries are written using OOP, so it’s a good tool to have in your Python toolkit.
OOP is aprogramming paradigm, or a specific way of designing a program. It allows us to think of the data in our program in terms of real-worldobjects, with both properties and behaviors. These objects can be passed around throughout our program.
Properties define the state of the object. This is the data that the object stores. This data can be a built-in type likeint
, or even our own custom types we’ll create later.
Behaviors are the actions our object can take. Oftentimes, this involves using or modifying the properties of our object.
00:00Hi. I’m Austin Cepalia with realpython.com,and I welcome you to our series on object-oriented programmingin Python 3.This is one of the biggest and most important subjects in all of programmingand as you can see, we have a lot to cover.
00:16This series will provide you with a basic conceptual understanding ofobject-oriented programming so that you can take your Python programming skills to thenext level. To fully appreciate the subject,we have to first understand why we need it. Programs are very complex thingsand there’s a hundred different ways that you could define what a program isformally,but basically, all programs are a set of instructions for manipulating data insome meaningful way. When I teach Python to kids,I always have them write a program that calculates how many days old they are,and that’s a great example of a program that follows these three steps. First,the program has to ask the user for their birth date.
00:59That’s the data it’s going to accept.Then, it manipulates the data by subtracting the current date from the user’sbirth date. Finally,the program displays the result of this calculation on the screen.
01:12That’s a great starter program, but it’s pretty trivial.How do we manage large programs with lots of data to keep track of, like a videogame? As you might have guessed,that’s where object-oriented programming comes in.
01:26Object-oriented programming,orOOP for short, is a very common programming paradigm. Informally,this means that it’s a specific way of designing programs, like a style that’swidely used in software projects.
01:41Not every program should be written with OOP but many are,and lots of languages support it.A lot of the Python code you interact with on a daily basis will be writtenusing OOP. Understandingthis is a fundamental part of becoming a great Python programmer.
01:57It opens the door to creating really cool software. At the heart of OOP is theidea that a program is composed of lots of individual objects.You can think of these objects as entities, orthings, in your program.
02:12Oftentimes, they are the nouns in your project, such as a person,a house, an email, or any other entity like that.These objects are like little bundles of data that are passed around throughoutthe life of your program. For example,a person might have properties like name, age, and home address.
02:33This is the individual data that the object stores throughout its life in theprogram. When we create thisPerson
object,we’ll make sure to give it a name, an age, and an address. Objects also havebehaviors. These are the actions that the object can take.
02:50Our person should be able to walk, talk, and breathe, for example.Oftentimes, these behaviors will need to reference the properties of the object.For example,if we make our talk behavior print the name and the age of the person,then ourPerson
object would need to know what its name and age is—thoseproperties we gave it earlier before.
03:12If this all seems a little bit confusing right now, don’t worry.When I started programming,this was the first real hurdle I ran into, and it just took some dedication andpractice until I finally understood it.
03:22We’ll make heavy use of objects throughout the rest of this seriesand you’ll love the feeling you get when it just clicks for you.
03:30In the next video,we’ll learn how to define our own objects using Python classes.I’ll see you there.
Peter T onMarch 22, 2019
Austin did an excellent job of clearly explaining the various OOP topics in all 7 videos, without leaving any gaps or questions. Now, I’m anxiously waiting for the next set of OOP videos to continue to this course & I hope they show up soon. IMHO, there aren’t enough OOP topics in realpython.com or dbader.org, but there are tons of other Python-related topics in both sites!
Dan BaderRP Team onMarch 22, 2019
Thanks for your feedback Peter, that’s great to hear. Stay tuned for more Python OOP tutorials and video courses in the future :)
Austin CepaliaRP Team onMarch 22, 2019
Thanks for the kind words Peter, I’m glad to hear the course was helpful for you.
arnautovdmitry2015 onApril 1, 2019
It’s very helpful course. Absolutely nice. Thanks a lot!
Sillikone onAug. 4, 2021
Hello ! Thanks for the course !I was exercising and wanted to create an instance attribute of type ‘Wear’ for my dogs. For it to work I had to explicitly name the class in theinit of Dog():
def__init__(self,name,age,cloth=Wear):
I’m affraid that I won’t be able to understand… but why this doesn’t work like the other types?
def__init__(self,name,age,cloth):
Bartosz ZaczyńskiRP Team onAug. 5, 2021
@Sillikone Hi, there. Can you clarify your question, please? I’m afraid folks over here may not be able to understand it correctly.
If you want to create an instance variable of a specific type, then you should state that explicitly in your.__init__()
method:
classDog:def__init__(self,name,age):self.name=nameself.age=ageself.cloth=Wear()# <--- Here
However, your function accepts thecloth
as a parameter, so perhaps this is what you meant instead?
classDog:def__init__(self,name,age,cloth):self.name=nameself.age=ageself.cloth=cloth
Sillikone onAug. 5, 2021
@Bartosz Zaczyński Hi ! Sorry for the trouble, yersterday this wasn’t working:
classWear:def__init__(self,color):self.color=colorclassDog:def__init__(self,name,cloth):self.name=nameself.cloth=clothshirt=Wear('red')dog=Dog('Luigi',shirt)print(f'{dog.name} is wearing a{dog.cloth.color} cloth')
I had to specify this for it to work:
classDog:def__init__(self,name,cloth=Wear):self.name=nameself.cloth=cloth
Rewritting all from scratch to clarify, I could not reproduce the behavior.
Become a Member to join the conversation.
Course Contents