Skip to content
CBT Nuggets
DemoBook a Demo

Understand Object-Oriented Programming Basics

This skill covers the foundational concepts of Object-Oriented Programming (OOP) in Python, including classes, instances, and inheritance. Learners will understand how to define classes, create instances, and work with attributes and methods. The skill also delves into more advanced topics such as instance methods, class methods, static methods, and reflexion using functions like isinstance() and issubclass(). A hands-on project to build a Library Management System is included to solidify these concepts.

Full skill from PCPP1. Preview the training IT teams trust.

Updated

1h 4m

Skill 1 of 58 in PCPP1

What you'll learn

  • Explain how a Python class acts as a blueprint that defines shared attributes and methods for the objects created from it
  • Describe how inheritance lets a subclass reuse the attributes and methods of a superclass to reduce code you write and maintain
  • Define a class with an __init__ method that sets initial attribute values, then access and change instance attributes using dot notation
  • Add instance methods to a class and distinguish them from class methods and static methods
  • Use isinstance and issubclass to check an object's type before acting on it and handle non-matching variables gracefully
  • Build a library management system with Book and Library classes that add, check in, and check out books

Free 64-minute video lesson with a full transcript and knowledge checks. No account needed to watch.

Essential Terms & Concepts

Before we get started with the core Object-Oriented syntax in Python, let's take a few minutes to familiarize ourselves with the basic terms and concepts of Object-Oriented Programming.

Knowledge Check

__________ represent the data that instances of a class contain.

Inheritance, Subclasses, and Superclasses

Next, since one of the major benefits of Object-Oriented Programming lies in its ability to help us form hierarchies of concepts, let's briefly discuss the idea inheritance in Object-Oriented Programming, as well as the terms "superclass" and "subclass" and what they mean in Python.

Knowledge Check

If we extend a class called "Car", then our new class is a _________ of "Car"

Basic Class, Instance, and Attribute Syntax

In this video, we'll dive into the core of object-oriented programming in Python – classes and instance - and see how to define classes, create instances, and work with attributes.

Knowledge Check

The _________ method in a class allows us to initialize the instance attribute values when we create a new instance of the class.

Basic Instance Method Syntax

Methods are the backbone of class behavior. Learn the syntax for defining and using methods within Python classes. We'll explore instance methods, class methods, and static methods, each serving unique purposes in your programming projects.

Knowledge Check

Which of the following demonstrates how to call an instance method on an instance of a class?

Reflexion Basics: isinstance and issubclass

Reflexion is a powerful feature that allows Python code to examine and modify its own structure. We'll take a much deeper look at reflexion later on in the course, but in this video, we'll focus on two essential functions – isinstance() and issubclass() and discover how these functions help you determine relationships between objects and classes dynamically.

Knowledge Check

If we extend class A to create class B, and then extend class B to create class C, calling "issubclass(C, A)" will return ________.

Challenge: Build a Library Management System

Time for a hands-on challenge! In this video, we'll take a look at a stimulating project – building a Library Management System using Python - which will give you the chance to apply your knowledge of classes, inheritance, and methods to create a simple system for managing books and in a library.

Solution Walk-Through: Build a Library Management System

Finally, let's walk through the solution to the Library Management System challenge together. We'll go step-by-step, explaining our thought process and implementation choices. By the end of this video, you'll have a complete understanding of how the system works and be ready to tackle similar projects confidently.

Knowledge Check

How much experience did you have with Object-Oriented Programming prior to this skill?

This interactive assessment is available in the full learning experience.

Want to answer questions like this yourself?
with no purchase required. Already have an account?

View Transcript

Essential Terms & Concepts

0:03All right, so we're gonna get started here by talking about some of the essential terms, as well as just essential concepts in general of object oriented programming in Python. And before we get started, the first thing that I wanna say is we're not going to get into the basic syntax in Python code just yet. We're just gonna be talking about the concepts here. And once we understand those concepts, that's when we will actually see how to express them in Python. So that's the first thing.

0:34The second thing, the second thing is that even if you already think you know object oriented programming basics, right? If you've done object oriented programming in another programming language, perhaps, or even if you've already done it in Python, I highly recommend that you at least watch this video on, let's say, 3x speed or 4x speed, just to make sure that you don't come across any concepts that you aren't already familiar with. Because what I've found is that Python actually contains some distinctions, right, some differences in how it does object oriented programming versus some other programming languages out there.

1:12And I'll try and point these out as we get to the concepts. So anyway, with those two things out of the way, let's jump into learning the basic terms and concepts of object-oriented programming. The first thing that we're going to talk about, which is really the center of the object-oriented universe, so to speak, is a class, right? So the class is basically a blueprint that we can use to create objects in our programs that behave in a similar way.

1:43And to help you remember this fact, I'm going to write the word class here in blue, because classes are blueprints. So what a class does, right, the way that it actually functions as a blueprint is by providing a standardized set of data attributes, all right? So we'll write that over here, attributes, as well as behaviors which are usually created in the form of something called methods, right?

2:14And methods, as you'll see, are basically just functions that belong to some aspect of a class. And the way that this works is whenever we create a new object from a class, that object is going to have the attributes and methods that the class defined. So we'll just call this an object here, all right? Now there's actually a special term for the relationship between an object and the class that we created it from, and that word is instance, right?

2:46So when we say that an object is an instance of a class, all that we really mean is that it was created using the class as a blueprint. So to give you a more real world example of this, and I'm gonna try and keep the color scheme the same here, so that you can relate this example to those concepts we just learned, if we were to create a Person class, well, what this Person class would do is define a set of attributes as well as behaviors that we want all people in our program to have in common.

3:18So on the attributes side of things, which I'm just going to write as "ATTRS," just to abbreviate, because that's kind of a long word to write out, we might have things like eye color, we might have things like hair color, and of course, we'd probably want attributes such as the person's name and age, okay? So these would be the basic attributes that we would give to our person blueprint. And again, the idea is that every object that we create from this blueprint now would have those attributes.

3:51So over on the behaviors side, which as we've established, is usually defined using something called methods, which we'll take a look at the syntax for shortly, on the methods side of things, we would want to define behaviors that we want all of our People objects to have in common. So in this case, we might have a method called greet, right, because we want all of our people to be able to greet each other somehow. We might have methods like eat or sleep, and we might also have a method called something like have_a_birthday, right?

4:27And this would obviously have an effect on the age attribute over here. So that's kind of the idea of methods, is that oftentimes they'll interact with these attributes and either use the values that they contain or modify the values that they contain. So anyway, now that we've defined some concrete attributes and methods for our Person class, if we were to create an object from this class, right, and as we've already discussed the correct terminology for this, is creating an instance of the class.

4:57If we were to create an instance of this class, which we'll just call something like Person1, well that person is going to have its own values for each of these attributes, right? So this person might have brown eyes, brown hair, and their name might be Shaun, just to use an example that's very close at hand, and their age might be 123 or something like that. All right, so this would be the first instance that we created from the Person class. If we were to create another instance, the key concept here is that this new instance, which we'll just call Person2, would have again, its own values for these attributes, which might be different and probably are different from all of the other person instances that we've created, all right?

5:44So this person might have green eyes and red hair, and this person's name might be something like Sue, and this person's age might be, I don't know, let's say 40, all right? So hopefully this example is helping you to understand the basic concepts of classes and instances and objects, and how each of these concepts is related to the other ones. So I hope this has been informative for you, and I'd like to thank you for viewing.

Inheritance, Subclasses, and Superclasses

0:03All right, at this point we've talked about most of the main object-oriented concepts that I wanted to talk about just to get ourselves started. But before we move on, one more concept that I want to discuss very briefly because this is a concept that we'll go into much more detail on a little bit later, is the idea of superclasses and subclasses in object oriented programming. Now, both of these concepts fall under the sort of larger umbrella of the concept of inheritance in object oriented programming.

0:39Now, the basic idea of inheritance is that we're able to create more specific classes from more general classes and, by doing this, we're able to reduce the amount of code that we have to write and maintain. So to give you a simple example of this, again, we'll go into much more detail on how inheritance works in general, as well as superclasses and subclasses in another section. But just to give you a general idea of how this might work, let's go back to our person example.

1:09So, again, we have a person class with its own attributes and methods. Well, if we wanted to create a more specific version of this person class for some sort of specific use case, for example, then what we can do is something called extending this class. Let me just write that here, we'll just say extend. And in this way, we'd be able to add extra attributes and extra methods onto this person class under a different name.

1:41All right, so let's say that we wanna create a new class that extends this person class, and we wanna create a software developer class. All right so, software developers, very special type of person. Let's think of some attributes that a software developer might have in addition to a person, right? Regular attributes that you wouldn't expect a regular person to have. Well, the first attribute that I can think of is programming languages, right? What programming languages does this developer know?

2:14All right so, that would be one example of an attribute that probably we wouldn't want to add to our person class, because most people don't know any programming languages. So that would just be an extra attribute that, in most cases, would never get used. Now, as far as extra methods, right, and you can probably think of extra attributes as well. So feel free to add those in, or write those down if you want. Just for the sake of example, extra methods or behaviors that this developer might have that a regular person most likely wouldn't have would be something like, well, the simple example of write code, all right?

2:52That's something that a regular person probably wouldn't do and therefore, that's something that we would only want our developer class to have. All right now, in this example that we've just gone through here, the developer class is what's referred to as a subclass of the person class. And, on the flip side, the person class is what's referred to as a superclass of the developer class so subclass and superclass really refer more to a relationship than a concrete construct in object oriented programming.

3:28And one more thing that I wanna mention is, if we were to go and create instances from these classes now, we can create an instance directly from the person class, right, in which case we'll just call this instance Person 1, all right? And, in this case, Person 1 would only have the attributes and methods that were defined in the person class. They wouldn't have any of the attributes or methods defined in the developer class. And, on the same token, if we were to create an instance of this developer class, which we'll just call something like Dev 1, well, this instance is going to have all of the attributes and methods from the person class plus all of the attributes and methods from the developer class.

4:12So, as you can see, this can really end up saving us a lot of code because if we want our developers to have all of the attributes and methods that a basic person has, which we do, right, that's just how developers work, we're people too, what we'd have to do without this subclass, superclass concept, right, the concept of inheritance is we'd have to actually define each of those attributes and methods on both of these classes. So we'd have to basically just create two classes from scratch and define all of those attributes, right?

4:46So you can probably picture them, I'm not going to list them all out here, but things like name, age, hair color, eye color, and so on, and all of the methods, right? Greet, eat, sleep, et cetera, we'd have to define all of those same methods on our developer as well. So we'd have to, again, add the name attribute, the age attribute, hair color, eye color, et cetera, and all of the basic methods such as greet, eat, sleep, which developers have to do as well, believe it or not, to our developer, in addition to the extra attributes and methods that we want to add to the developers.

5:23So in other words, these classes would really be separate entities so that if we were to add a new method or attribute to our person, right, let's say that we wanna add a new attribute called favorite color to our person class, well, obviously we'd want our developers to have that as well so what we'd have to do then is we'd have to go over to our developer class and add the same attribute, right? We'd have to essentially duplicate that attribute ourselves.

5:53And, as you can probably imagine, in larger programs this would create a tremendous amount of work, which is why this whole idea of subclasses, superclasses, inheritance, right, is so important. So anyway, that's pretty much all the essential terms and concepts that I wanted to cover for object oriented programming. The next thing that we're gonna take a look at is how we can actually use, how we can actually express all of these concepts in Python. And we're also going to see a few further distinctions that Python makes with regards to things like attributes and methods.

6:28So I hope this has been informative for you, and I'd like to thank you for viewing.

Basic Class, Instance, and Attribute Syntax

0:03Awesome, so now that we've finally gone through all of the basic terminology and concepts for Object-Oriented Programming in Python, at least the introductory concepts and terminology, the next thing that we're gonna do is we're gonna see how to express all of those concepts in code in Python. So what I'm gonna do here is I'm just gonna use Visual Studio Code to write out all of my examples. You can obviously use whatever your IDE of choice is. Or if you're more comfortable using something like a Jupyter Notebook, feel free to do that as well.

0:36But you know, the code that I'll write in here should be exactly the same for you, no matter what you use. So what I'm gonna do is I'm gonna create a new file and we'll call this something like oop.basic.py. And now that we've done that, let's get started with Python's Object-Oriented Syntax. The first thing that you're gonna wanna know how to do is define a class in Python. And if I were to ask you to guess how you define a new class, you'd probably get pretty close because the way that we do this in Python is by saying class and this is followed by the name of the class that we wanna define.

1:15So if we wanted to define a person class here, we would just say class person, and then this would be followed by a colon. And after this is where what's called the body of the class is defined. And the simplest possible way that you can define this is by writing the keyword pass. Now, in case you're not familiar with this pass keyword in Python, basically all that it does is it says that we want to come back and define this later. It's sort of like the equivalent of in other programming languages that have brackets to define things like classes and functions, just having an opening bracket and a closing bracket and leaving them empty, right.

1:56But since Python depends on indentation to do this, we actually have to write that pass keyword underneath it. And now that we've defined this very simple person class, let's take a look at how to create an instance of this class in Python. Whenever we wanna create a new object using a specific class as a blueprint, right. Whenever we want to create an instance as that's called of a class, we started off like a regular variable, right. So just like we're able to say X = five, for example, we can say person_1 =.

2:30And the way that we tell Python that we want to create a new instance of this person class is just by calling the person class like a function. All right, so we just put two parentheses after the name of the class and that creates a new instance for us. And well, now that we've created one instance, we might as well create another one. So we'll just say, person_2 = Person (). And from here on, you can practice this as many times as you want, right. Right, so you can create person_3, person_4 et cetera.

3:00And again, just to draw out sort of a graphical representation of what's going on here, each time we say that something equals this class name with parentheses, after it, we're taking the class, we're telling Python that we wanna use this class as a blueprint and we're creating a new instance of that class in Python. So person_1 is over here and person_2 is over here. And another thing to notice, especially if you work occasionally with other programming languages, or if Python isn't your first programming language, another thing to notice is that Python doesn't have a new keyword, right.

3:45So in most other programming languages that I've seen, whenever you wanna create a new instance of an object, you have to say something like = new_person. All right and Python doesn't do that, presumably just to simplify things. So anyway, now that we've seen how to define a basic class, very, very basic class, this is literally as simple as it could possibly get. And we've also seen how to create instances of the class. Let's talk now about how to make this class do something. So the first thing that we're going to discuss is a very important method that all classes can have, and this is called the _init_method.

4:23All right, now the _init_method, I'm just going to call it the _init_method, but whenever I say that, keep in mind that it does have two underscore characters before it and after it. And as you'll see in a future section, these two underscore characters before and after a method name indicate that this method has some sort of special significance in Python, right. And it also prevents us from accidentally overriding these methods if we wanted to create a method of our own called _init_, right.

4:54So anyway, the _init_method, which by the way, we do have to define with def in front of it, right. So we define methods in Python just like we define regular functions. And this _init_method is always going to take at least one argument. And that argument is called (self). Now, we'll come back to the (self) argument shortly. But what we're gonna do inside this _init_method is we're just going to say print, and we'll print out something that says ("Creating a new instance of the Person class!"), okay.

5:31So at this point, you might be wondering what the _init_method does. Well, if we run this code, which we can do just by saying python3 oop-basics.py we'll see that this method actually gets executed twice. And that's because the _init_method in Python will automatically get called whenever we create a new instance of a class, right. So each time we call Person() with parentheses after it, that's going to call this _init_method and execute whatever code it contains.

6:02All right, now, another piece of terminology that we haven't mentioned yet is this _init_method is what's known in Object-Oriented Programming as a constructor, right, or a constructor method. All right and the basic idea is that its main purpose is to put together or construct a new instance of this object in whatever way we define, hence the name constructor. So now that we know what this _init_method is, let's circle back to this (self) argument. Or, I've been calling this an argument, but I guess it's technically a parameter since the parameters are what we define when we define the function.

6:39And the arguments are what we actually pass in when we call the function. That's just a little distinction that most people that I've heard don't actually make in their speech. And if you watch any of my other courses, you'll notice that I make the same mistake. But since this exam does actually pay attention to that distinction, I just thought it would be a good idea to point that out. So let's talk about this (self) parameter in the _init_method. The (self) parameter refers to the specific instance of this class that we're in the process of creating.

7:12And after we've created the instance we can use this (self) parameter to do things like access attributes and call methods on the specific instance. That might sound a little bit confusing, so let me just show you how this works. Let's say that we want to start giving this Person class some attributes, right, just some of the attributes that we talked about previously, like name, age, hair color, eye color. Well, the way that we can do that is by using this (self) parameter.

7:42So what we're gonna do is underneath this call to print, we're gonna say self and then in order to give this instance a new attribute, we can just say self.name, right. So this is us giving it a name attribute. And then we can say = and we can give that attribute whatever value we want. And then we can do the same with whatever other attributes we want to add. So if we wanna add an age attribute, we can say self.age = 123. self.hair_color = "brown".

8:16And self.eye_color = "brown" as well. All right, so what this does is whenever we create a new instance of this Person class, it's going to automatically add these attributes, name, age, hair, color and eye color to that instance with these values. Now, what that currently means is that both of these instances that we're creating, person_1 and person_2 will have the same values for all of those attributes. Now, since most of the time that's not what we want, what Python actually allows us to do is define further parameters, right, extra parameters in this _init_method after the (self) parameter that we can then use to set specific values for these different attributes.

9:01All right, so in our case here, what we could do is add a name parameter, an age parameter, a hair color parameter and an eye color parameter. And then we could just use those to set the initial values of these attributes. So we could say self.name = name, self.age = age, self.hair_color = hair_color, and self.eye_color = eye_color. And now that we've added these extra parameters, that's actually going to change the way that we have to create a new person instance, right.

9:34What we're gonna do is actually pass in the values that we want for name, age, hair color and eye color. So if we want person_1 to be me, right, I'm just gonna use myself for this example. You should feel free to use your own details because, well, frankly, it would be a little bit strange if you were to use my details for yourself. So what we would do is we would just say, Shaun and then for the age we would say 123. And then for the hair color we would say brown. And then for the eye color we would say brown, okay.

10:06And for this other person, if we wanted this other person to have other attributes, let's just use my fellow CBT Nuggets Trainer, Lalo Nunez as our second example here. So for the name we put Lalo for the age, I don't happen to know Lalo's exact age, but I feel like he's probably not 123 years old. So let's write 102. All right and then for hair color and eye color, we're going to say brown. And I believe his eyes are brown. Strangely enough, I haven't memorized his eye color. And now that we've defined these two instances of our Person class, let's take a look at how to actually access these attributes from the instances, from the variables that we've assigned these new instances to.

10:49Well, that actually looks a lot like what we saw up here with (self), except since we're referring to these instances outside of the class, we're going to replace (self) with whatever the name is of the variable we assign this instance to. So for instance, if we wanted to know person_ 1's name, we would just say person_1.name, okay. And likewise, if we wanted to know person_1's age, we could say person_1.age or person_1.hair_color, or person_1.eye_color, you get the idea.

11:20And the same thing would go for person_2. If we were to say person_2 and we wanted to access person_2's name, we could say person_2.name. All right, so just to give you a quick example, let's say something like print, and we'll say, we'll use a template string here by putting the (f) before the quotation marks. And we'll say person_1.name and then we'll say is and then we'll access the person's age. So {person_1.age} years old.

11:52And then we'll do the same thing for person_2. So we'll say print and then we'll use a template string again. So we'll say (f), and then person_2.name is and then we'll say {person_2.age} years old. And if we save and run our program again, we'll see that sure enough, when we create those two instances of our Person class, we see that this creating a new instance of the person class gets printed out to the console.

12:22And then after this, we see Shaun is 123 years old, and Lalo is very young at only 102 years old. One last thing that I wanna show you how to do before we move on to other topics is modify the attributes on a class instance. And that's actually pretty easy, right. You can for the most part, treat these attributes when we say person_one.name or person_one.age in the same way that you'd treat another variable. So if we wanted to change Lalo's name to Lala, well, all we would've to do in that case is say, person_two.name = Lala.

12:58And of course, if we were to print this second print statement out again and save and run our code, we'll see that sure enough that changes the value of person_2's name attribute. So anyway, just to quickly review what we've covered in this video. We learned the basic syntax for creating a new class, right. And to do that, all we need to do is use the class keyword and select a name for our class. And by the way, classes in Python always start with a capital letter. And that's actually not required by Python Syntax, but it's something that you will always want to do, right.

13:33It's a very well established convention that class names are just supposed to start with a capital letter. And this really helps you know later on down the line that something is in fact a class and not just a function. So anyway, we saw how to create classes and we also saw how to create instances of those classes just by creating a new variable and saying equals, and calling that class in the same way that we would call a function. And then we learned about the _init_method and how it's run automatically every time we create a new instance.

14:03And we also talked about this (self) parameter, which we can use to set attributes on an instance among other things. And we also saw that we could define other parameters in this _init_method that would allow us to set initial values for those attributes, right. So we added name, age, hair color and eye color. And we were then able to pass them in when we created those new instances. And last but not least, we saw how to both access and change attributes on specific instances by accessing those attributes using the dot notation.

14:40So anyway, I hope this has been informative for you. And I'd like to thank you for viewing.

Basic Instance Method Syntax

0:03Alright, so now that we're familiar with the basic syntax of defining classes as well as working with attributes, the next thing that we're gonna take a look at in more detail is how to add instance methods to our classes. Now, the reason that I specify instance methods, which basically just refers to the fact that these methods belong to individual instances of a class instead of to the class itself, is because there are in fact other types of methods such as class methods and static methods, which is something that we'll talk about in a later section.

0:40So these instance methods that we're gonna take a look at are pretty easy to define, and in fact, we've already seen how to define one of them when we defined this init method on our person class. Now, the init method on our person class, as I already said, is basically called whenever we want to create a new instance of a class. And it allows us to do some initial setup by, you know, creating attributes using this self parameter that the init method contains.

1:12So let's take a look at how to define some other methods that we can call on our person class, or rather on instances of our person class. So the basic syntax is just like the way that you would normally define a function. So we're gonna use the def keyword and after that is going to come the name of the method. So let's just create a method called greet. And the interesting thing about instance methods in Python, is that they're all going to take this self parameter as their first parameter.

1:46And the reason that they do this is because this self keyword actually gives the method a reference to the individual instance. So that if we want to access an attribute or make some sort of change to an attribute inside this method, we can do so. So in the case of greet, right, let's say that we want this method to do something very simple like print out a greeting to the console, right? So here's what that would look like. We could just say hello and to make things a little bit more interesting, let's say that we want to use the person's name, right?

2:20The individual instances name attribute in this string as well, right? So we could say something like, hello, my name is, and then we'd have the name of the person inside that string. So first of all, we need to convert this to a template string just by putting a lowercase F before the string. And then, inside these curly braces, we can just say self dot name. Alright? So in other words, this self parameter is how we access different attributes on this individual instance, and this is actually a little bit different from other programming languages there, most programming languages just provide this sort of this keyword.

3:03Or in some other programming languages they call it self as well, that will just automatically refer to the surrounding instance that it's in. So JavaScript is like this, Java's like this C plus plus is like this, you get the idea. But anyway, this is the basic way that we define instance methods for our classes in Python. And the good news is that using these methods is pretty straightforward as well, right? It's pretty much just as straightforward as accessing an attribute on an instant.

3:35All right? So if we wanted to have person one and person two greet each other, well all we would have to do in that case, and by the way, I'm just going to comment out these lines here. You can delete them or I would recommend leaving them around for reference, but just to get them outta the way for now, I'll comment them out so that they're not cluttering up the console. And if we wanna have person one and person two greet, we can just say person one, and then just like the syntax for accessing attributes on an instance in Python, we just say dot greet, and of course we have to put parentheses after it.

4:08Now, something that can kind of throw newcomers off here is the fact that we define this greet method with a single parameter called self, but when we call it, we don't actually pass an argument into it, right? So that's just something that Python does automatically for us behind the scenes is it passes the argument for self to this greet method, but we just call it with empty parentheses if that's the only parameter that that function takes. So let's make person two greet as well.

4:40We're just gonna say person two dot greet. And now if we save our code and run our program, sure enough, we'll see, that it prints out hello, my name is Shaun and hello, my name is Lalo. Now one last thing that I wanted to go over here is what the syntax is for passing extra arguments to an instance method in Python. Because if we wanted to add an extra argument, let's say for the name of the person that this person is actually greeting, well what that would look like is we could just say something like other name, and here we'll use snake case for that, just to make it a little bit more stylistically correct.

5:18And now that we've done that, what we're gonna do is we're going to add that other name into the string here. So we'll just say hello, and we'll insert the other name parameter. And now the syntax for calling this greet method, would look something like this. If we wanted person one to greet Lalo, and person two to greet Shaun, we would just add those strings and pass them as arguments to those methods. So if we run our code again after saving it, we'll see that sure enough that those values get incorporated into what gets printed out to the console.

5:51And really what I wanted to demonstrate here is just that even though this other name parameter is the second parameter that we've defined in our greet method here, it's actually the only one, right? It becomes the first one when we call that method, because again, we don't actually pass this self value ourselves. Python does that behind the scenes. So anyway, that's the basic idea of working with instance methods in Python. I hope this has been informative for you, and I'd like to thank you for viewing.

Reflexion Basics: isinstance and issubclass

0:03All right, so now that we've covered the basic syntax of defining classes as well as defining instance attributes and instance methods, the last thing that we're gonna take a look at here before we jump into a hands-on example, which should be a lot of fun, is taking a look at two very important functions that Python provides us with for doing something that's often referred to as reflexion, and it is spelled a little bit differently than reflection like what you see in a mirror. It's spelled with an X, which I think is kind of funny, but basically the idea here is reflexion allows us to actually look at instances and classes and tell how they're related to each other inside our code.

0:45So right now we only have one class called Person and we've been creating instances from it. Now one thing that you might wanna do in Python, and one thing that you probably will wanna do at some point if you want to make your code as robust as possible, is check to see whether a given variable is in fact an instance of a specific class. So let's say that we define a function here whose sole purpose is to take some sort of Person object as an argument.

1:16Well we might call that something like print_person_details, and of course, this is going to take a person as an argument, so we'll add a person parameter there. All right, and what this is going to do, of course, is it would just print out basic information like the person's name, age, hair color, and eye color. But just to make it simple, we'll say something like print, and then in a template string here, we'll say something like name, then we'll print out the person's name by saying person.name.

1:47And then we'll put a comma and we'll say age, we'll print out person.age. And you can continue on with the other attributes. But the main point here is in this case, we would in fact want to make sure that this argument, right, that whatever argument the calling code is passing in for this person parameter is actually a person. And in that case, we need to look no further than Python's isinstance function. All right, so the isinstance function written all in lowercase, which is a little bit strange because personally I would expect it to have an underscore in between is and instance.

2:24But what do I know? This isinstance function, the main purpose of it is to tell us whether or not a given variable is an instance of a class. So in our case here, if we wanted to print out the details for this person, we'd wanna check to see if the value being passed in is an instance of the Person class. So what that would look like is we would just say if isinstance, and then the first argument here is the object we want to check, so we would say person. And the second argument here is the class that we want to see if this thing is an instance of, so we would just pass our Person class like so.

3:03Okay, so that would be how we would protect this code down here from making some sort of mistake if this was just a number or a string or something like that. And if we wanted to provide some sort of backup here, we could just say something like print, and then we'd say something like, "Oh no! It looks like you\'re," and by the way, we have to put this little backslash before the single quote here so that it doesn't end the string. And then we'll just say, "Trying to print details for a non-person variable."

3:37Okay, and now if we were to call this thing, right, if we try and say print_person_details and we'll try and print out person_1's details. And by the way, let's just comment out some of this code down here where we did the person_1.greet just so that we're not cluttering up the console. And if we run our code now, what we'll see is that sure enough, it prints out the details of person_1. But if we try and pass something that's not an instance of the Person class, it would just print out this warning message, right?

4:07So if we try and say something like x = 10, and then say print_person_details(x), well, what we'll see if we run our code again is sure enough, it says, "Oh no! It looks like you're trying to print details for a non-person variable." So that's the isinstance function, and one thing that you should know about it is if we've created some sort of class hierarchy, in other words, if we have subclasses and superclasses in our program, it's important to know that isinstance will return true even when an object isn't directly an instance of a given class.

4:43Let me show you what I mean by that. What we're gonna do here is we're just going to define another class here called Developer, very similar to the example that I gave at the beginning of this section, and we're gonna make this class extend the Person class. So in other words, we're gonna make this class a subclass of the Person class. And in order to do that in Python, the syntax looks like this. We just put parentheses after the subclass' name, and we tell it what we want the superclass to be.

5:15So in our case, that would be Person. And by the way, this is something that we'll cover in much more detail in another section, but I just wanted to show you here so that we could go into a little more detail on this isinstance function and some of the caveats of it. So we're just gonna say pass, right? We're not gonna worry about implementing this subclass right now because that's really not the point here. But if we now create a developer, right? So let's say that we change person_1 here to a Developer. Well, you might be surprised to find that this isinstance function is still gonna return true for person_1 when we ask it if it's an instance of the Person class, right?

5:54Because even though person_1 is now directly an instance of the Developer class, it's also considered to be an instance of the Person class. So in other words, right, if we draw out our little class tree here, all right, so we have Person up at the top, and we're extending Person when we create our Developer class, and then we're creating an instance of this Developer class, which we're calling person_1, but really it's just me, right? So I'll just write Shaun here.

6:24So just to be clear, when we ask Python if Shaun here is an instance of the Developer class, well, it's going to return true, right? Because it's a direct instance of that class. And you know, when we created person_1 here, right, when we created this instance, we actually used the Developer constructor to do that. However, it's also considered to be an instance of this Person class because the Developer class is a subclass of that Person class. So that might get a little bit confusing here.

6:56Really, all you need to know is that isinstance just asks whether this class is somewhere in this object's class hierarchy, right? It's not asking whether it's a direct instance of that class. So anyway, that's the isinstance function. And before we move on, there's one other function that I wanna talk about, which is actually going to be very convenient to demonstrate now that we have this Developer subclass, and that is a function called issubclass.

7:27Now this one is basically just used to tell whether one class is a subclass either directly or indirectly, right? If there's a class or two in between those two classes in the hierarchy of some other class. All right, so in our case here, Developer would be a subclass of this Person class, and we can test that out by, we'll just print it out to the console, I guess to make it easy for ourselves. We'll just say, "Developer is a subclass of Person," and then we'll print out the value that the issubclass function returns, all right?

8:05So let's just fill in Developer, right? The first argument is going to be the class that we want to check, and the second argument is going to be the class that we want to tell if the first argument is a subclass of. So let's just run our code here, and sure enough, we'll see that it says, "Developer is a subclass of Person: True." And if we flip this around, right, if we ask if let's say Person is a subclass of Developer, well, we're gonna see that it now returns false.

8:38Okay, and again, another thing to keep in mind here is if we were to have a slightly larger class hierarchy, so if we were to have a class that extends the Developer class, we'll call this something like PythonDeveloper, and this is going to extend the Developer class, and we'll just put pass inside of that class because we don't want to implement anything just yet. Well, if we ask Python if the PythonDeveloper class is a subclass of the Person class, right?

9:10And that's what the syntax for that would be, let's just try this here. Sure enough, we're gonna see that that returns true. And by the way, I haven't been updating this message here. The main thing that you'll wanna pay attention to is the true or false value here. All right, so the point is that just like with our isinstance function, how it would return true even if an object wasn't a direct instance of a class, the issubclass function is going to return true even when a class is further down in the class hierarchy.

9:43All right, so this doesn't mean that PythonDeveloper directly extends the Person class, but it does mean that the Person class is somewhere in the PythonDeveloper class' ancestry or hierarchy, if you want to call it that. So anyway, those are the isinstance and issubclass functions. I hope this has been informative for you, and I'd like to thank you for viewing.

Challenge: Build a Library Management System

0:03All right. So now that we've learned all about the basic object-oriented terms, concepts, and syntax in Python, it's time for you to prove it to yourself by solving a challenge. Now, the way that this is gonna work is I'm going to give you a simple challenge that will incorporate all of the concepts that we've talked about in this section. And I recommend that you take probably anywhere from five to 10 minutes to try and solve the challenge, and once you've given it a good try, you can go on to my solution walkthrough video, where I'll actually show you how I went about solving this challenge myself.

0:42Okay, so the basic challenge is going to be this. You're gonna use the concepts that we learned about in this section to create a library management system. And first of all, when I say library, I'm talking about the kind with books in it, not the kind of library that you install into your code base. And to give you a few more details about this challenge, you're gonna be creating two main classes. The first one is going to be a library class, which will represent the entity that is in charge of managing books, right?

1:15And the second class is going to be a book class. Now, here's your task. You're gonna create these classes and they're gonna have to have the following criteria. The book class is gonna have to have a few attributes that books normally have. So it's gonna have a title attribute, of course, an author attribute, as well as an attribute called something like number of pages. Okay, and it's really up to you if you want to add extra attributes if they make sense to you, but with this book class, the other thing that's gonna be important for you to do is allow yourself to specify an initial value for each of these attributes through the books in knit method.

1:54All right? So in other words, you should be able to specify the title, author and number of pages when you create a new book. So that's the book class that you're gonna be creating. Over here on the library side of things, the library class is going to be in charge of managing books. And what this means is that it's going to have an attribute, which will be called something like books, right? And this will just be a list or whatever other iterable you want to use of all of the books that are in the library.

2:25And additionally, the library is going to have four methods. The first method is gonna be a method called Add Book. And this is going to be the equivalent of the library buying a new book and putting it on their shelves. And additionally, it will also have a method called Remove Book, which will be the equivalent of a library deciding that a book is not in good condition anymore and getting rid of it. So we've got methods for adding and removing books from a library instance.

2:57And additionally, the library class should have two other methods for allowing library visitors to check books in and check books out. So what that means is your library class is also gonna have methods called Check-In, that of course, will take a book as an argument. And I haven't been writing those in these parentheses here. The parentheses are really just to show that these are methods, and you'll also have a method called Check-Out. Cool. So anyway, that's the basic task here. This should really help you solidify the main concepts that we covered in this section.

3:30So again, give this five to 10 minutes to write out your code, and once you're done, take a look at the solution video where I'll show you how I implemented all of this code. So I hope this has been informative for you, and I'd like to thank you for viewing.

Solution Walk-Through: Build a Library Management System

0:02Alright, so hopefully you've given the challenge at least an honest try and taken maybe 10 minutes or so to try and implement all of the things that we talked about. And now that you've done that, I'm gonna show you how I solved this challenge. So the first thing that I did obviously was I created a new file and I ended up calling this file library-mgmt.py. Okay, so once I was in here, I started off by just creating both classes just to get them in place. So I created the Book class and then I just wrote pass in there just to get the Book class written out.

0:38And then I created the Library class and did pass inside of there as well. So going back to the Book class, now that we've got both of these in place, the Book class, as we said, needed to have a few attributes and we also needed the ability to set all of those attributes through the init method. So what I did is I removed the pass keyword and I added the init method. And then as you hopefully remembered, the first parameter that the init method as well as any instance method in Python classes has to take is self, a reference to the current instance that we're dealing with here.

1:16And then we added title, author and number of pages, which I just abbreviated as num_pages. Cool, so inside this init method now, all I did was I took the title, author, and num_pages parameters, and I used those to set the initial values of the attributes. So we said self.title = title and then self.author = author and self.num_pages = num_pages.

1:48And that's pretty much all we need to do, right? I didn't print out something inside this init method because, well, that's generally not considered to be a good practice unless you need it for debugging or something like that. So now that I did that, I moved on to the Library class. And the Library class is a little bit interesting because as we said, it's really only going to have one attribute, and that is an attribute keeping track of all of the books that are in the library. So what I did for this is I just defined another init method inside this library, and then we started off with self and that was in fact the only parameter that I added to the Library classes init method.

2:29And then inside here I started off by creating a books attribute on the Library class or on instances of the Library class that is, and I set that to an empty list, right? So I just created a new empty list that we could later on add books to. And that's pretty much all I did in the init method for the library. So the next two methods that I defined were the two methods for adding or removing a book from the library.

2:59And this is more in the case of the library buying new books or throwing books out. So what I did is I started off by defining an add_book method. And of course since this is an instance method, we had to define self as its first parameter. And then the second parameter was book, right? That's the book that we wanted to add to the library. And all I did just to start off with was I said self.books.append and I appended that book onto the library's list of books, the books instance attribute of the library.

3:37And that's pretty much it for the add_book method. So I moved on after that to the remove_book method. And what I did for this is I just said self and I used the title of the book as sort of a unique identifier. Now this was just a design decision that I made. You could have just said book like so, and passed in an actual instance of the book. But I just used the title because of how I implemented the body of this book method. What I did is I said self.books and then I actually used a list comprehension to remove any books in the library with that title.

4:17So I said self.books = and don't worry if you need to dust off your list comprehension skills here. I'm just going to show you what I wrote. So I said book in Self.books if book.title is not equal to title. Alright, so basically I just got all of the books from self.books whose title did not match the title parameter here. And oops, I actually wrote this wrong. This should be book for book in self.books.

4:50Okay, so got a little bit thrown off by the list comprehension syntax. But anyway, that's what I have, and that's all I did for the remove_book methods. Alright, so the next two methods maybe threw you off a little bit because what they required us to do was think about how we wanna keep track of whether a book is currently checked out or not. So what I did here, and this is really just one option that you could have gone with, there are really other ways that you could have done this, but what I actually ended up doing was adding another instance attribute to the Book class by saying self and then I added an attribute called status that could either be available or something like checked out.

5:35So now the idea is that whenever we want to check a book in or check a book out, all we have to do is change that status attribute on the book. So here's what that would look like. We would just say define, and then we could say check_in. And this would of course take self as the first parameter. And I again just used the title of the book that we want to check in or out. And again, this is just a design decision. If you made it so that these methods actually took a book as the second parameter, that's just fine as well as long as you got it to work.

6:10So for this check-in method now, what we're gonna do is we're gonna start off by finding the book with this title. And the way that I did that was by using Python's built-in Next method. But you could have done this in just the same way with a for loop. So I said book = next and then I used a list comprehension. So I said book for book in self.books. So very similar to what I did in this remove_book method that I defined.

6:40And then I said if book.title is equal to title, and then I provided a backup value here of None, so that if there were no books that match that title, we wouldn't end up raising some sort of exception. So anyway, now that we found the book, the next thing that I did was I said, if book is not None, so basically if we found a matching book with that title, then what we did is we said book.status and we set that to available, right?

7:12Because this is the check_in method. And I didn't add an else clause to this if statement because that's really kind of beyond the scope of this challenge. But if you went ahead and did something like that to handle the case where that book isn't actually in the library, that's all right too. In fact, that's just great. Cool, so the last thing that we had to do was define a check_out method. And this one actually looked very similar to the check_in method. So I made this take a self parameter and a title parameter.

7:43Again, you might have done a book as the second parameter there. And I just did the same basic thing as in the check_in method for this checkout_method. I found the book and provided None as the backup value, but in this case I set the book status to checked_out, or you could have set this to unavailable or whatever. Basically just something indicating that this book is no longer available in the library. So anyway, that's pretty much all we had to do for creating the book and Library classes.

8:17If you went so far as to actually test these two classes that you created, which I would highly recommend as a rule of thumb, here's what that might look like. We would create a new library, we'll just call that something like my_library = Library. And this would just initialize the self.books attribute to an empty list. And then we could add books to this library by saying my_library.add_book. And what you could do here is you could either create a book separately by saying something like book_1 = Book and then filling out all of the different arguments there.

8:57But what you could also do is just create that new book in place. All right, so if you don't need a reference to this exact book later on in the program, this would be the way that you could go about doing that. And here what I did is I just said title. So we could have something like "Pride and Prejudice," and this is by Jane Austen. And let's just say that this has something like 279 pages. Alright, so that would add a book to our library, and let's just add a few more books here for the sake of demonstration.

9:31So the next book could be something like "War and Peace" by Leo Tolstoy. And this is a very long book, something on the order of like 1400 pages, let's say. And for our last book here, let's say "The Great Gatsby," and this is by F. Scott Fitzgerald. So we'll type that out here. You might have used slightly less classy books here, but that's okay too. And for this one, let's say something like 150 pages.

10:03Cool, so now that we've added books to our library, the next thing that we can do is try checking out a book. So that would look something like this. We could say something like my_library.check_out and we'll try checking out "Pride and Prejudice" here. So we'll say "Pride and Prejudice," because again, the way that I implemented the Library class was we had to actually use the title as the unique ID. And if I were to go back and do it again, I might not do it that way because there is a chance that two or more books have the same title.

10:34But anyway, that'll be fine for now. So that would check out the "Pride and Prejudice" book. And in fact, in order to verify that this is actually doing what it's supposed to be doing, what we're gonna do is we're going to define an actual variable to hold this. We'll just say book_1 = and oops, I didn't mean to do that. We'll try that again, book_1 = and then I'll paste that there, there we go. And now we can say add_book(book_1) And after we check out "Pride and Prejudice," let's just print out the status of "Pride and Prejudice."

11:07So we'll say status, and then we'll change this to a template string. So we'll say book_1.status. Cool, so at this point, let's try saving and running our code. So we're just gonna say python3 library-mgmt.py and sure enough, we see that the status is, oops, it looks like I wrote checkout-out. I'm not sure why I did that. That should be checked_out. So let's just try that one more time to make sure I fixed it, and sure enough, we see status checked_out.

11:37So let's check that book back in by saying my_library.check_in and we'll check "Pride and Prejudice" back in, of course. So we'll say "Pride and Prejudice," and then we'll print out the status of that book again. So sure enough, it's back in the library and its status is available. And last but not least, let's say that "Pride and Prejudice" has been checked out and checked back in so many times that its binding is now all tattered and the library wants to get rid of it. Well, in that case, we could just say my_library.remove_book and we would remove it by its title, "Pride and Prejudice" one more time.

12:16And that would remove it from the library. And if you wanted to see whether that was the case, you could always just say print and you could print out my library.books if you wanted to. And sure enough, we would see that there's only two books in there. So this is not exactly in human readable form, but at least we know that there are only two books in the library left, which would seem to suggest that we were successful in removing our books. So anyway, this is just how I went about solving this challenge. Hopefully this allowed you to stretch your brain a little bit and solidify the skills that we learned in this section.

12:49So I hope this has been informative for you, and I'd like to thank you for viewing.

Frequently asked questions

What does the Understand Object-Oriented Programming Basics lesson cover in Python?

The lesson explains how a Python class acts as a blueprint that defines shared attributes and methods, and how inheritance lets a subclass reuse a superclass's attributes and methods to reduce code the team writes and maintains. It then moves into syntax: defining a class with an __init__ method, accessing and changing instance attributes with dot notation, adding instance methods, and using isinstance and issubclass to check an object's type.

How long does the Python object-oriented programming basics lesson take?

The lesson runs 1 hour 4 minutes, including a hands-on challenge the trainer suggests spending five to 10 minutes on before watching the solution walk-through. Training Managers should note it is one part of a PCPP1 course whose 40 listed modules alone total about 36.5 hours.

Should developers who already know OOP from another language still take this lesson?

Yes. The trainer recommends that even experienced developers watch at least at higher playback speed, because Python has distinctions in how it handles object-oriented programming compared with other languages, and he points these out as each concept comes up.

What hands-on project does this Python OOP lesson include?

Learners build a library management system with a Book class that sets title, author, and number of pages through its __init__ method, and a Library class that manages a collection of books with methods to add, check in, and check out books. The trainer then walks through his own solution so the team member can compare approaches.

How does this lesson relate to the PCPP1 certification exam PCPP-32-101?

This lesson is part of the Certified Professional in Python Programming 1 (PCPP1) course, which prepares learners for the Python Institute's Professional-level exam PCPP-32-101. It lays the class, attribute, method, and inheritance groundwork that later course sections build on, including class methods, static methods, and deeper inheritance topics.

How can an IT Director roll out this PCPP1 course to a development team?

IT Directors can assign the course to developers who need to standardize practices around OOP and other advanced Python features, using CBT Nuggets Playlists to sequence the rollout by role or sprint priority. Team reporting then shows per-learner progress and completion, with completion exports available for audit evidence.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need PCPP1?

Learning on your own? Browse individual plans ($49/month, billed annually) →

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo