Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Basics: Object-Oriented Programming (Summary)

Now you’re ready to use object-oriented programming (OOP) to write more readable and maintainable code in Python! With OOP, you can create blueprints for objects that contain both data and behaviors.

In this video course, you’ve learned how to:

  • Create a class
  • Use classes to create new objects
  • Instantiate classes with attributes and methods

To learn more about OOP, check out:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 So you’re at the end of Object-Oriented Programming. That was a lot of information. And again, you have to practice this stuff for it to really sink in. A lot of it might seem like, what do I use that for?

00:10 Or in what cases is that useful? Can you give me a real-life application for this? And there are loads. You can use it everywhere. But it really won’t start to click until you yourself start using it or reading other people’s code and understanding why they’re using it in this case and how it’s being used. You’ve learned about the object-oriented programming paradigm.

00:32 Now, paradigm is just a fancy word for saying style or way of doing things. You’ve defined a class. You’ve added attributes to that class. You’ve added methods. You’ve used objects created from classes.

00:47 If you’re looking to dive in a bit deeper, Real Python has some additional resources for you to get stuck into. For instance, Object-Oriented Programming (OOP) in Python 3, which is a very in-depth article about everything you want to know about object-oriented programming in Python 3.

01:04 That itself also has another video course that goes into more detail than this one. You have Getters and Setters: Manage Attributes in Python. You’ll have seen how in the class instances in this course, you’ve been referencing attributes and setting attributes with dot notation and assignment directly, but the getting and setting of these attributes is a big topic.

01:27 If you’re curious about more of that and how to customize the behavior of that—maybe every time you get an attribute, you want something to happen. More likely, it is when you set an attribute you want something to happen.

01:38 Maybe it needs to update other attributes. Check out that article. And finally, we’ve got Operator and Function Overloading in Custom Python Classes. Now, this is the dunder methods we are talking about, where you can customize how your instances behave with certain operators, like + (addition), - (subtraction), == (equals to), > (greater than), < (less than). Now, Real Python’s content on object-oriented programming, classes, and instances is not limited to these three, but is a huge subject.

02:12 And that was an introduction to object-oriented programming. Thank you for watching and following along.

Avatar image for risko619

risko619 on July 19, 2023

I really didnt understand this, nor was I able to run the exercise. Feeling a bit overwhelmed with all this stuff now

Avatar image for Martin Breuss

Martin Breuss RP Team on July 20, 2023

@risko619 aah, yes OOP is a complex programming topic that can totally take a while to sink in. Best thing to do is to keep reading or watching content that covers it, and just mess around trying to solve the challenges. Even if you don’t get it all the way, putting in some practice time and (figuratively) banging your head against that OOP wall helps to make it click eventually.

Is there anything specific that you got stuck with? Did you check out the other resources that Ian mentioned in this lesson?

Avatar image for Sneha Nath

Sneha Nath on Sept. 16, 2023

Excellent course and very informative! Thank you so much.

Avatar image for alphafox28js

alphafox28js on Nov. 19, 2023

I wish they would have explaned how to print all the functions of the class at with one iteration…

class Dog:
    species = "Canis Familiaris"
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return (f"{self.name}, {self.age}")
        Pepper = Dog('Pepper',4)

    # Instance Method:
    def description(self):
        return (f"{self.name} is {self.age} years old, and she is the greatest dog alive!")
        Pepper.description()

    def speak(self, sound):
        return f"{self.name} says {sound}"
        Pepper.speak()

Pepper = Dog('Pepper',4)
Pepper.description()
Pepper.speak('Woof! Woof!')
Avatar image for Martin Breuss

Martin Breuss RP Team on Nov. 22, 2023

I’m not sure I correctly understood your question, but generally, you can inspect a Python object using dir().

That gives you everything though, so you might want to filter the results, e.g. like shown below if you’re only looking for instance methods you defined in the class:

print(
    [
        method
        for method in dir(Pepper)
        if callable(getattr(Pepper, method))
        and not method.startswith("__")
    ]
)

When you run this code on the Pepper object you created above, then you’ll get the two instance method names as a result:

['description', 'speak']

Alternatively, you could also look into the built-in inspect module.

Did this answer your question, or did you mean something else?

Avatar image for alphafox28js

alphafox28js on Nov. 23, 2023

Good Morning @Martin Breuss… So basically, when trying to instantiate the Attributes of the class via Instances. I keep running into a problem when separting Main_Class vs SubClass vs code variable assignments.

example. would be something like this… New Window_Script 1: Main_Class

class Animal:
    species = "Mammals"

    def __init__(self, name, age): #type: [Canine. breed: breed of canine, internal_temp: warm/cold blooded.]
        self.name = name
        self.age = age

# Instance Methods:
    def __str__(self):
        return f"{self.name} is {self.age} years old."

    def speak(self, sound):
        return f"Sound of {self.name}; {sound}."

class Pitbull(Animal):