Skip to content
CBT Nuggets

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 lesson from PCPP1. Preview the IT training 23,000+ organizations trust.

1h 4m 7 Videos 6 Questions

Skill 1 of 58 in PCPP1

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.

  1. AAttributes
  2. BMethods
  3. CConstructors
  4. DDators

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

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"

  1. Asubclass
  2. Bunderclass
  3. Csuperclass
  4. Dparent class

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

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.

  1. A__init__
  2. B__create__
  3. C__setup__
  4. D__start__

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

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?

  1. Aa.some_method()
  2. Ba[some_method]()
  3. Ca->some_method()
  4. Da(some_method)

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

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 ________.

  1. ATrue
  2. BFalse
  3. CNone
  4. Dit will raise an exception

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

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.

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

View Transcript

Essential Terms & Concepts

0:03<v Shaun>All right, so we're gonna get started here</v>

0:05by talking about some of the essential terms,

0:08as well as just essential concepts in general

0:11of object oriented programming in Python.

0:14And before we get started,

0:16the first thing that I wanna say

0:17is we're not going to get

0:19into the basic syntax in Python code just yet.

0:24We're just gonna be talking about the concepts here.

0:26And once we understand those concepts,

0:29that's when we will actually see

0:31how to express them in Python.

0:33So that's the first thing.

0:34The second thing, the second thing

0:36is that even if you already think you know

0:39object oriented programming basics, right?

0:41If you've done object oriented programming

0:43in another programming language, perhaps,

0:45or even if you've already done it in Python,

0:48I highly recommend that you at least watch this video

0:50on, let's say, 3x speed or 4x speed,

0:54just to make sure that you don't come across

0:57any concepts that you aren't already familiar with.

1:00Because what I've found

1:01is that Python actually contains

1:04some distinctions, right,

1:05some differences in how it does object oriented programming

1:09versus some other programming languages out there.

1:12And I'll try and point these out

1:14as we get to the concepts.

1:16So anyway, with those two things out of the way,

1:18let's jump into learning the basic terms and concepts

1:22of object-oriented programming.

1:23The first thing that we're going to talk about,

1:25which is really the center of the object-oriented universe,

1:29so to speak, is a class, right?

1:33So the class is basically a blueprint

1:37that we can use to create objects

1:39in our programs that behave in a similar way.

1:43And to help you remember this fact,

1:45I'm going to write the word class here in blue,

1:49because classes are blueprints.

1:51So what a class does, right,

1:53the way that it actually functions as a blueprint

1:57is by providing a standardized set

1:59of data attributes, all right?

2:02So we'll write that over here, attributes,

2:06as well as behaviors which are usually created

2:11in the form of something called methods, right?

2:14And methods, as you'll see, are basically just functions

2:16that belong to some aspect of a class.

2:20And the way that this works is

2:22whenever we create a new object from a class,

2:26that object is going to have the attributes

2:29and methods that the class defined.

2:32So we'll just call this an object here, all right?

2:36Now there's actually a special term for the relationship

2:40between an object and the class

2:42that we created it from,

2:43and that word is instance, right?

2:46So when we say that an object is an instance of a class,

2:50all that we really mean

2:51is that it was created using the class as a blueprint.

2:55So to give you a more real world example of this,

2:59and I'm gonna try and keep the color scheme the same here,

3:01so that you can relate this example

3:03to those concepts we just learned,

3:05if we were to create a Person class,

3:08well, what this Person class would do

3:10is define a set of attributes as well as behaviors

3:14that we want all people in our program to have in common.

3:18So on the attributes side of things,

3:21which I'm just going to write as "ATTRS,"

3:24just to abbreviate,

3:25because that's kind of a long word to write out,

3:27we might have things like eye color,

3:31we might have things like hair color,

3:33and of course, we'd probably want attributes

3:35such as the person's name and age, okay?

3:40So these would be the basic attributes

3:42that we would give to our person blueprint.

3:45And again, the idea is that every object

3:47that we create from this blueprint now

3:49would have those attributes.

3:51So over on the behaviors side,

3:55which as we've established,

3:57is usually defined using something called methods,

4:00which we'll take a look at the syntax for shortly,

4:03on the methods side of things,

4:04we would want to define behaviors

4:06that we want all of our People objects to have in common.

4:10So in this case,

4:12we might have a method called greet, right,

4:15because we want all of our people

4:17to be able to greet each other somehow.

4:19We might have methods like eat or sleep,

4:23and we might also have a method

4:24called something like have_a_birthday, right?

4:27And this would obviously have an effect

4:29on the age attribute over here.

4:31So that's kind of the idea of methods,

4:34is that oftentimes they'll interact with these attributes

4:38and either use the values that they contain

4:40or modify the values that they contain.

4:43So anyway, now that we've defined

4:44some concrete attributes and methods

4:46for our Person class,

4:48if we were to create an object from this class, right,

4:51and as we've already discussed the correct terminology

4:53for this, is creating an instance of the class.

4:57If we were to create an instance of this class,

5:00which we'll just call something like Person1,

5:03well that person is going to have its own values

5:07for each of these attributes, right?

5:08So this person might have brown eyes, brown hair,

5:14and their name might be Shaun,

5:16just to use an example that's very close at hand,

5:19and their age might be 123 or something like that.

5:23All right, so this would be the first instance

5:25that we created from the Person class.

5:27If we were to create another instance,

5:29the key concept here is that this new instance,

5:32which we'll just call Person2,

5:34would have again, its own values for these attributes,

5:38which might be different

5:39and probably are different

5:40from all of the other person instances

5:43that we've created, all right?

5:44So this person might have green eyes and red hair,

5:49and this person's name might be something like Sue,

5:51and this person's age might be,

5:55I don't know, let's say 40, all right?

5:57So hopefully this example is helping you

5:59to understand the basic concepts

6:01of classes and instances and objects,

6:05and how each of these concepts

6:07is related to the other ones.

6:09So I hope this has been informative for you,

6:11and I'd like to thank you for viewing.

Inheritance, Subclasses, and Superclasses

0:03<v Shaun>All right, at this point we've talked about</v>

0:05most of the main object-oriented concepts

0:08that I wanted to talk about just to get ourselves started.

0:12But before we move on, one more concept

0:15that I want to discuss very briefly

0:17because this is a concept that we'll go

0:19into much more detail on a little bit later,

0:22is the idea of superclasses and subclasses

0:26in object oriented programming.

0:29Now, both of these concepts fall

0:30under the sort of larger umbrella of the concept

0:35of inheritance in object oriented programming.

0:39Now, the basic idea of inheritance is that we're able

0:41to create more specific classes from more general classes

0:47and, by doing this, we're able to reduce the amount of code

0:51that we have to write and maintain.

0:53So to give you a simple example of this,

0:55again, we'll go into much more detail

0:57on how inheritance works in general,

1:00as well as superclasses and subclasses in another section.

1:04But just to give you a general idea of how this might work,

1:07let's go back to our person example.

1:09So, again, we have a person class

1:12with its own attributes and methods.

1:15Well, if we wanted to create a more specific version

1:18of this person class for some sort

1:21of specific use case, for example, then what we can do

1:24is something called extending this class.

1:28Let me just write that here, we'll just say extend.

1:32And in this way, we'd be able to add extra attributes

1:36and extra methods onto this person class

1:40under a different name.

1:41All right, so let's say that we wanna create

1:43a new class that extends this person class,

1:46and we wanna create a software developer class.

1:50All right so, software developers,

1:52very special type of person.

1:54Let's think of some attributes

1:56that a software developer might have

1:58in addition to a person, right?

2:00Regular attributes that you wouldn't expect

2:03a regular person to have.

2:05Well, the first attribute that I can think of

2:07is programming languages, right?

2:09What programming languages does this developer know?

2:14All right so, that would be one example of an attribute

2:17that probably we wouldn't want to add to our person class,

2:21because most people don't know any programming languages.

2:24So that would just be an extra attribute

2:26that, in most cases, would never get used.

2:29Now, as far as extra methods, right,

2:32and you can probably think of extra attributes as well.

2:34So feel free to add those in,

2:36or write those down if you want.

2:38Just for the sake of example, extra methods

2:41or behaviors that this developer might have

2:43that a regular person most likely wouldn't have

2:47would be something like, well,

2:49the simple example of write code, all right?

2:52That's something that a regular person probably wouldn't do

2:56and therefore, that's something that

2:57we would only want our developer class to have.

3:00All right now, in this example that we've just gone

3:02through here, the developer class is

3:05what's referred to as a subclass of the person class.

3:10And, on the flip side, the person class

3:13is what's referred to as a superclass

3:17of the developer class so subclass

3:19and superclass really refer more

3:21to a relationship than a concrete construct

3:26in object oriented programming.

3:28And one more thing that I wanna mention is,

3:30if we were to go and create instances

3:32from these classes now, we can create

3:34an instance directly from the person class, right,

3:38in which case we'll just call

3:40this instance Person 1, all right?

3:43And, in this case, Person 1 would only have the attributes

3:46and methods that were defined in the person class.

3:49They wouldn't have any of the attributes

3:51or methods defined in the developer class.

3:55And, on the same token, if we were to create

3:57an instance of this developer class,

3:59which we'll just call something like Dev 1,

4:03well, this instance is going to have all of the attributes

4:06and methods from the person class plus all of the attributes

4:10and methods from the developer class.

4:12So, as you can see, this can really end up saving us a lot

4:15of code because if we want our developers to have

4:18all of the attributes and methods that a basic person has,

4:22which we do, right, that's just how developers work,

4:25we're people too, what we'd have to do

4:27without this subclass, superclass concept, right,

4:31the concept of inheritance is we'd have

4:33to actually define each of those attributes

4:35and methods on both of these classes.

4:38So we'd have to basically just create two classes

4:41from scratch and define all of those attributes, right?

4:46So you can probably picture them,

4:48I'm not going to list them all out here,

4:50but things like name, age, hair color,

4:54eye color, and so on, and all of the methods, right?

4:57Greet, eat, sleep, et cetera, we'd have to define

5:00all of those same methods on our developer as well.

5:03So we'd have to, again, add the name attribute,

5:06the age attribute, hair color, eye color, et cetera,

5:09and all of the basic methods such as greet, eat, sleep,

5:13which developers have to do as well,

5:15believe it or not, to our developer,

5:18in addition to the extra attributes

5:20and methods that we want to add to the developers.

5:23So in other words, these classes

5:25would really be separate entities

5:27so that if we were to add a new method

5:30or attribute to our person, right,

5:32let's say that we wanna add a new attribute

5:34called favorite color to our person class,

5:39well, obviously we'd want our developers

5:41to have that as well so what we'd have to do then

5:44is we'd have to go over to our developer class

5:46and add the same attribute, right?

5:50We'd have to essentially duplicate that attribute ourselves.

5:53And, as you can probably imagine, in larger programs

5:56this would create a tremendous amount of work,

5:58which is why this whole idea of subclasses,

6:00superclasses, inheritance, right, is so important.

6:05So anyway, that's pretty much all the essential terms

6:08and concepts that I wanted to cover

6:10for object oriented programming.

6:11The next thing that we're gonna take a look at

6:13is how we can actually use, how we can actually express

6:18all of these concepts in Python.

6:20And we're also going to see a few further distinctions

6:24that Python makes with regards

6:26to things like attributes and methods.

6:28So I hope this has been informative for you,

6:31and I'd like to thank you for viewing.

Basic Class, Instance, and Attribute Syntax

0:03<v Shaun>Awesome, so now that we've finally gone through</v>

0:05all of the basic terminology and concepts for

0:09Object-Oriented Programming in Python,

0:11at least the introductory concepts and terminology,

0:15the next thing that we're gonna do is we're gonna see

0:17how to express all of those concepts in code in Python.

0:21So what I'm gonna do here is I'm just gonna use

0:23Visual Studio Code to write out all of my examples.

0:27You can obviously use whatever your IDE of choice is.

0:30Or if you're more comfortable using something like

0:32a Jupyter Notebook, feel free to do that as well.

0:36But you know, the code that I'll write in here

0:38should be exactly the same for you,

0:40no matter what you use.

0:42So what I'm gonna do is I'm gonna create a new file

0:45and we'll call this something like oop.basic.py.

0:50And now that we've done that, let's get started

0:52with Python's Object-Oriented Syntax.

0:55The first thing that you're gonna wanna know

0:57how to do is define a class in Python.

1:01And if I were to ask you to guess how you define

1:05a new class, you'd probably get pretty close

1:07because the way that we do this in Python is

1:09by saying class and this is followed by the name

1:13of the class that we wanna define.

1:15So if we wanted to define a person class here,

1:18we would just say class person,

1:20and then this would be followed by a colon.

1:23And after this is where what's called

1:25the body of the class is defined.

1:28And the simplest possible way that you can

1:30define this is by writing the keyword pass.

1:34Now, in case you're not familiar with this pass keyword

1:36in Python, basically all that it does is it says that

1:40we want to come back and define this later.

1:42It's sort of like the equivalent of in other

1:45programming languages that have brackets to define things

1:49like classes and functions, just having an opening bracket

1:53and a closing bracket and leaving them empty, right.

1:56But since Python depends on indentation to do this,

2:00we actually have to write that pass keyword underneath it.

2:03And now that we've defined this very simple person class,

2:07let's take a look at how to create

2:09an instance of this class in Python.

2:11Whenever we wanna create a new object

2:13using a specific class as a blueprint, right.

2:16Whenever we want to create an instance as that's called

2:20of a class, we started off like a regular variable, right.

2:23So just like we're able to say X = five, for example,

2:26we can say person_1 =.

2:30And the way that we tell Python that we want

2:32to create a new instance of this person class

2:36is just by calling the person class like a function.

2:40All right, so we just put two parentheses

2:42after the name of the class

2:44and that creates a new instance for us.

2:47And well, now that we've created one instance,

2:49we might as well create another one.

2:51So we'll just say, person_2 = Person ().

2:54And from here on, you can practice this

2:56as many times as you want, right.

2:58Right, so you can create person_3, person_4 et cetera.

3:00And again, just to draw out sort of a

3:02graphical representation of what's going on here,

3:05each time we say that something equals this class name

3:09with parentheses, after it, we're taking the class,

3:13we're telling Python that we wanna use this class

3:15as a blueprint and we're creating a new instance

3:19of that class in Python.

3:21So person_1 is over here

3:25and person_2 is over here.

3:30And another thing to notice, especially if you work

3:33occasionally with other programming languages,

3:35or if Python isn't your first programming language,

3:38another thing to notice is that Python

3:41doesn't have a new keyword, right.

3:45So in most other programming languages that I've seen,

3:48whenever you wanna create a new instance of an object,

3:51you have to say something like = new_person.

3:54All right and Python doesn't do that,

3:56presumably just to simplify things.

3:58So anyway, now that we've seen how to

4:00define a basic class, very, very basic class,

4:03this is literally as simple as it could possibly get.

4:07And we've also seen how to create instances of the class.

4:10Let's talk now about how to make this class do something.

4:13So the first thing that we're going to discuss is

4:17a very important method that all classes can have,

4:20and this is called the _init_method.

4:23All right, now the _init_method, I'm just going to

4:25call it the _init_method, but whenever I say that,

4:27keep in mind that it does have two underscore characters

4:30before it and after it.

4:33And as you'll see in a future section, these two

4:36underscore characters before and after a method name

4:40indicate that this method has some sort of

4:42special significance in Python, right.

4:45And it also prevents us from accidentally

4:47overriding these methods if we wanted to create

4:51a method of our own called _init_, right.

4:54So anyway, the _init_method, which by the way,

4:56we do have to define with def in front of it, right.

5:00So we define methods in Python

5:02just like we define regular functions.

5:05And this _init_method is always going

5:07to take at least one argument.

5:09And that argument is called (self).

5:11Now, we'll come back to the (self) argument shortly.

5:15But what we're gonna do inside this _init_method

5:17is we're just going to say print,

5:19and we'll print out something that says

5:22("Creating a new instance of the Person class!"), okay.

5:31So at this point, you might be wondering

5:32what the _init_method does.

5:35Well, if we run this code, which we can do just

5:37by saying python3 oop-basics.py we'll see that

5:42this method actually gets executed twice.

5:45And that's because the _init_method in Python will

5:47automatically get called whenever we create

5:50a new instance of a class, right.

5:53So each time we call Person() with parentheses after it,

5:57that's going to call this _init_method

5:59and execute whatever code it contains.

6:02All right, now, another piece of terminology that

6:03we haven't mentioned yet is this _init_method is

6:06what's known in Object-Oriented Programming as a

6:10constructor, right, or a constructor method.

6:13All right and the basic idea is that its main purpose

6:16is to put together or construct a new instance

6:21of this object in whatever way we define,

6:24hence the name constructor.

6:26So now that we know what this _init_method is,

6:28let's circle back to this (self) argument.

6:31Or, I've been calling this an argument, but I guess

6:34it's technically a parameter since the parameters

6:36are what we define when we define the function.

6:39And the arguments are what we actually

6:41pass in when we call the function.

6:43That's just a little distinction that most people

6:46that I've heard don't actually make in their speech.

6:48And if you watch any of my other courses,

6:50you'll notice that I make the same mistake.

6:52But since this exam does actually pay attention

6:55to that distinction, I just thought it would be

6:56a good idea to point that out.

6:59So let's talk about this (self) parameter

7:02in the _init_method.

7:04The (self) parameter refers to

7:06the specific instance of this class

7:09that we're in the process of creating.

7:12And after we've created the instance we can use this

7:15(self) parameter to do things like access attributes

7:19and call methods on the specific instance.

7:23That might sound a little bit confusing,

7:24so let me just show you how this works.

7:26Let's say that we want to start giving this

7:29Person class some attributes, right,

7:31just some of the attributes that we talked about previously,

7:34like name, age, hair color, eye color.

7:38Well, the way that we can do that is

7:39by using this (self) parameter.

7:42So what we're gonna do is underneath this call to print,

7:46we're gonna say self and then in order to give this instance

7:49a new attribute, we can just say self.name, right.

7:54So this is us giving it a name attribute.

7:56And then we can say = and we can give

7:59that attribute whatever value we want.

8:01And then we can do the same with whatever

8:03other attributes we want to add.

8:04So if we wanna add an age attribute,

8:06we can say self.age = 123.

8:10self.hair_color = "brown".

8:16And self.eye_color = "brown" as well.

8:21All right, so what this does is whenever we

8:23create a new instance of this Person class,

8:26it's going to automatically add these attributes,

8:29name, age, hair, color and eye color

8:31to that instance with these values.

8:34Now, what that currently means is that both

8:37of these instances that we're creating,

8:40person_1 and person_2 will have the same values

8:43for all of those attributes.

8:45Now, since most of the time that's not what we want,

8:47what Python actually allows us to do is define

8:50further parameters, right, extra parameters in this

8:53_init_method after the (self) parameter that

8:56we can then use to set specific values

8:59for these different attributes.

9:01All right, so in our case here, what we could do is

9:02add a name parameter, an age parameter,

9:05a hair color parameter and an eye color parameter.

9:09And then we could just use those to set

9:11the initial values of these attributes.

9:13So we could say self.name = name, self.age = age,

9:19self.hair_color = hair_color,

9:21and self.eye_color = eye_color.

9:26And now that we've added these extra parameters,

9:29that's actually going to change the way that

9:31we have to create a new person instance, right.

9:34What we're gonna do is actually pass in the values

9:37that we want for name, age, hair color and eye color.

9:40So if we want person_1 to be me, right,

9:43I'm just gonna use myself for this example.

9:45You should feel free to use your own details

9:48because, well, frankly, it would be a little bit strange

9:50if you were to use my details for yourself.

9:53So what we would do is we would just say,

9:55Shaun and then for the age we would say 123.

9:59And then for the hair color we would say brown.

10:02And then for the eye color we would say brown, okay.

10:06And for this other person, if we wanted this other person

10:08to have other attributes, let's just use my fellow

10:12CBT Nuggets Trainer, Lalo Nunez as our second example here.

10:16So for the name we put Lalo for the age,

10:18I don't happen to know Lalo's exact age,

10:20but I feel like he's probably not 123 years old.

10:24So let's write 102.

10:27All right and then for hair color and eye color,

10:29we're going to say brown.

10:30And I believe his eyes are brown.

10:32Strangely enough, I haven't memorized his eye color.

10:35And now that we've defined these two instances of our

10:38Person class, let's take a look at how to actually

10:41access these attributes from the instances, from the

10:45variables that we've assigned these new instances to.

10:49Well, that actually looks a lot like what we saw

10:51up here with (self), except since we're referring to

10:55these instances outside of the class, we're going to

10:58replace (self) with whatever the name is

11:01of the variable we assign this instance to.

11:04So for instance, if we wanted to know person_ 1's name,

11:07we would just say person_1.name, okay.

11:11And likewise, if we wanted to know person_1's age,

11:14we could say person_1.age or person_1.hair_color,

11:18or person_1.eye_color, you get the idea.

11:20And the same thing would go for person_2.

11:23If we were to say person_2 and we wanted to

11:25access person_2's name, we could say person_2.name.

11:29All right, so just to give you a quick example,

11:30let's say something like print, and we'll say,

11:34we'll use a template string here by putting the (f)

11:36before the quotation marks.

11:38And we'll say person_1.name and then we'll say is

11:45and then we'll access the person's age.

11:47So {person_1.age} years old.

11:52And then we'll do the same thing for person_2.

11:53So we'll say print and then

11:55we'll use a template string again.

11:57So we'll say (f), and then person_2.name is

12:03and then we'll say {person_2.age} years old.

12:09And if we save and run our program again,

12:11we'll see that sure enough, when we create

12:13those two instances of our Person class,

12:17we see that this creating a new instance

12:19of the person class gets printed out to the console.

12:22And then after this, we see Shaun is 123 years old,

12:25and Lalo is very young at only 102 years old.

12:29One last thing that I wanna show you how to do

12:31before we move on to other topics is

12:33modify the attributes on a class instance.

12:37And that's actually pretty easy, right.

12:39You can for the most part, treat these attributes

12:42when we say person_one.name or person_one.age in the

12:46same way that you'd treat another variable.

12:48So if we wanted to change Lalo's name to Lala,

12:51well, all we would've to do in that case

12:53is say, person_two.name = Lala.

12:58And of course, if we were to print this

13:01second print statement out again and save

13:03and run our code, we'll see that sure enough

13:06that changes the value of person_2's name attribute.

13:10So anyway, just to quickly review

13:12what we've covered in this video.

13:13We learned the basic syntax for creating a new class, right.

13:17And to do that, all we need to do is use the

13:19class keyword and select a name for our class.

13:22And by the way, classes in Python

13:24always start with a capital letter.

13:27And that's actually not required by Python Syntax,

13:30but it's something that you will always want to do, right.

13:33It's a very well established convention that class names

13:36are just supposed to start with a capital letter.

13:39And this really helps you know later on down the line

13:41that something is in fact a class and not just a function.

13:45So anyway, we saw how to create classes and we also saw

13:48how to create instances of those classes just by creating

13:51a new variable and saying equals,

13:53and calling that class in the same way

13:56that we would call a function.

13:57And then we learned about the _init_method and how

13:59it's run automatically every time we create a new instance.

14:03And we also talked about this (self) parameter,

14:06which we can use to set attributes on

14:09an instance among other things.

14:11And we also saw that we could define other parameters

14:14in this _init_method that would allow us to set

14:17initial values for those attributes, right.

14:20So we added name, age, hair color and eye color.

14:23And we were then able to pass them in

14:25when we created those new instances.

14:28And last but not least, we saw how to both access

14:31and change attributes on specific instances by

14:36accessing those attributes using the dot notation.

14:40So anyway, I hope this has been informative for you.

14:42And I'd like to thank you for viewing.

Basic Instance Method Syntax

0:03<v ->Alright, so now that we're familiar with the basic syntax</v>

0:06of defining classes as well as working with attributes,

0:10the next thing that we're gonna take a look at

0:13in more detail is how to add instance methods

0:16to our classes.

0:18Now, the reason that I specify instance methods,

0:21which basically just refers to the fact that these methods

0:25belong to individual instances of a class

0:29instead of to the class itself,

0:31is because there are in fact other types of methods

0:34such as class methods and static methods,

0:37which is something that we'll talk about in a later section.

0:40So these instance methods that we're gonna take a look at

0:44are pretty easy to define,

0:45and in fact, we've already seen how to define one of them

0:48when we defined this init method on our person class.

0:53Now, the init method on our person class, as I already said,

0:55is basically called whenever we want to create

0:59a new instance of a class.

1:02And it allows us to do some initial setup by, you know,

1:06creating attributes using this self parameter

1:09that the init method contains.

1:12So let's take a look at how to define some other methods

1:16that we can call on our person class,

1:18or rather on instances of our person class.

1:22So the basic syntax is just like the way that you would

1:25normally define a function.

1:27So we're gonna use the def keyword and after that

1:31is going to come the name of the method.

1:33So let's just create a method called greet.

1:36And the interesting thing about instance methods in Python,

1:39is that they're all going to take this self parameter

1:43as their first parameter.

1:46And the reason that they do this is because

1:48this self keyword actually gives the method a reference

1:51to the individual instance.

1:54So that if we want to access an attribute

1:57or make some sort of change to an attribute

2:00inside this method, we can do so.

2:02So in the case of greet, right,

2:05let's say that we want this method to do something

2:08very simple like print out a greeting to the console, right?

2:11So here's what that would look like.

2:12We could just say hello and to make things a little bit

2:16more interesting, let's say that we want to use

2:18the person's name, right?

2:20The individual instances name attribute

2:24in this string as well, right?

2:26So we could say something like, hello, my name is,

2:30and then we'd have the name of the person

2:33inside that string.

2:34So first of all, we need to convert this to a template

2:37string just by putting a lowercase F before the string.

2:41And then, inside these curly braces,

2:44we can just say self dot name.

2:48Alright? So in other words,

2:49this self parameter is how we access different attributes

2:52on this individual instance,

2:54and this is actually a little bit different

2:56from other programming languages there,

2:59most programming languages just provide

3:01this sort of this keyword.

3:03Or in some other programming languages

3:05they call it self as well,

3:07that will just automatically refer to the surrounding

3:10instance that it's in.

3:12So JavaScript is like this,

3:14Java's like this C plus plus is like this, you get the idea.

3:18But anyway, this is the basic way that we define instance

3:22methods for our classes in Python.

3:24And the good news is that using these methods

3:27is pretty straightforward as well, right?

3:29It's pretty much just as straightforward

3:31as accessing an attribute on an instant.

3:35All right? So if we wanted to have person one

3:37and person two greet each other,

3:39well all we would have to do in that case, and by the way,

3:41I'm just going to comment out these lines here.

3:44You can delete them or I would recommend leaving them around

3:47for reference, but just to get them outta the way for now,

3:49I'll comment them out so that they're not cluttering

3:51up the console.

3:52And if we wanna have person one

3:54and person two greet, we can just say person one,

3:57and then just like the syntax for accessing attributes

4:01on an instance in Python,

4:03we just say dot greet,

4:05and of course we have to put parentheses after it.

4:08Now, something that can kind of throw newcomers off here

4:10is the fact that we define this greet method

4:14with a single parameter called self,

4:18but when we call it, we don't actually pass an argument

4:20into it, right?

4:22So that's just something that Python does automatically

4:24for us behind the scenes is it passes the argument

4:27for self to this greet method,

4:30but we just call it with empty parentheses

4:32if that's the only parameter that that function takes.

4:35So let's make person two greet as well.

4:40We're just gonna say person two dot greet.

4:42And now if we save our code and run our program,

4:46sure enough, we'll see, that it prints out

4:49hello, my name is Shaun and hello, my name is Lalo.

4:52Now one last thing that I wanted to go over here

4:54is what the syntax is for passing extra arguments

4:57to an instance method in Python.

5:00Because if we wanted to add an extra argument,

5:02let's say for the name of the person that this person

5:05is actually greeting, well what that would look like

5:09is we could just say something like other name,

5:12and here we'll use snake case for that,

5:14just to make it a little bit more stylistically correct.

5:18And now that we've done that,

5:19what we're gonna do is we're going to add

5:20that other name into the string here.

5:23So we'll just say hello,

5:25and we'll insert the other name parameter.

5:28And now the syntax for calling this greet method,

5:30would look something like this.

5:32If we wanted person one to greet Lalo,

5:35and person two to greet Shaun,

5:37we would just add those strings

5:38and pass them as arguments to those methods.

5:41So if we run our code again after saving it,

5:44we'll see that sure enough that those values

5:47get incorporated into what gets printed out to the console.

5:51And really what I wanted to demonstrate here

5:53is just that even though this other name parameter

5:56is the second parameter that we've defined

5:58in our greet method here,

6:00it's actually the only one, right?

6:02It becomes the first one when we call that method,

6:05because again, we don't actually pass

6:07this self value ourselves.

6:09Python does that behind the scenes.

6:11So anyway, that's the basic idea of working

6:14with instance methods in Python.

6:16I hope this has been informative for you,

6:17and I'd like to thank you for viewing.

Reflexion Basics: isinstance and issubclass

0:03<v Shaun>All right, so now</v>

0:04that we've covered the basic syntax of defining classes

0:07as well as defining instance attributes

0:10and instance methods,

0:12the last thing that we're gonna take a look at here

0:14before we jump into a hands-on example,

0:16which should be a lot of fun,

0:17is taking a look at two very important functions

0:20that Python provides us with

0:22for doing something that's often referred to as reflexion,

0:26and it is spelled a little bit differently than reflection

0:29like what you see in a mirror.

0:32It's spelled with an X, which I think is kind of funny,

0:34but basically the idea here is reflexion allows us

0:37to actually look at instances and classes

0:41and tell how they're related to each other inside our code.

0:45So right now we only have one class called Person

0:49and we've been creating instances from it.

0:52Now one thing that you might wanna do in Python,

0:55and one thing that you probably will wanna do at some point

0:57if you want to make your code as robust as possible,

1:01is check to see whether a given variable is

1:05in fact an instance of a specific class.

1:07So let's say

1:08that we define a function here whose sole purpose is

1:12to take some sort of Person object as an argument.

1:16Well we might call that something like print_person_details,

1:22and of course,

1:23this is going to take a person as an argument,

1:26so we'll add a person parameter there.

1:28All right, and what this is going to do, of course,

1:31is it would just print out basic information

1:33like the person's name, age, hair color, and eye color.

1:36But just to make it simple, we'll say something like print,

1:39and then in a template string here,

1:41we'll say something like name,

1:43then we'll print out the person's name

1:45by saying person.name.

1:47And then we'll put a comma and we'll say age,

1:50we'll print out person.age.

1:52And you can continue on with the other attributes.

1:55But the main point here is in this case,

1:57we would in fact want to make sure that this argument,

2:01right, that whatever argument the calling code is passing in

2:04for this person parameter is actually a person.

2:08And in that case,

2:09we need to look no further

2:10than Python's isinstance function.

2:15All right, so the isinstance function written all

2:17in lowercase, which is a little bit strange

2:19because personally I would expect it

2:21to have an underscore in between is and instance.

2:24But what do I know?

2:26This isinstance function, the main purpose

2:28of it is to tell us whether

2:29or not a given variable is an instance of a class.

2:33So in our case here,

2:34if we wanted to print out the details for this person,

2:38we'd wanna check to see if the value being passed

2:40in is an instance of the Person class.

2:43So what that would look like is we would just say

2:44if isinstance, and then the first argument here is

2:49the object we want to check, so we would say person.

2:52And the second argument here is the class

2:55that we want to see if this thing is an instance of,

2:59so we would just pass our Person class like so.

3:03Okay, so that would be

3:04how we would protect this code down here

3:07from making some sort of mistake if this was just a number

3:11or a string or something like that.

3:13And if we wanted to provide some sort of backup here,

3:15we could just say something like print,

3:17and then we'd say something like, "Oh no!

3:19It looks like you\'re," and by the way,

3:22we have to put this little backslash

3:24before the single quote here

3:25so that it doesn't end the string.

3:27And then we'll just say,

3:28"Trying to print details for a non-person variable."

3:37Okay, and now if we were to call this thing, right,

3:40if we try and say print_person_details

3:42and we'll try and print out person_1's details.

3:45And by the way,

3:46let's just comment out some of this code down here

3:49where we did the person_1.greet just

3:51so that we're not cluttering up the console.

3:53And if we run our code now,

3:54what we'll see is that sure enough,

3:56it prints out the details of person_1.

3:59But if we try and pass something

4:01that's not an instance of the Person class,

4:04it would just print out this warning message, right?

4:07So if we try and say something like x = 10,

4:10and then say print_person_details(x),

4:14well, what we'll see

4:15if we run our code again is sure enough, it says, "Oh no!

4:18It looks like you're trying to print details

4:19for a non-person variable."

4:21So that's the isinstance function,

4:24and one thing that you should know about it is

4:26if we've created some sort of class hierarchy,

4:30in other words, if we have subclasses

4:32and superclasses in our program,

4:34it's important to know

4:35that isinstance will return true even

4:38when an object isn't directly an instance of a given class.

4:43Let me show you what I mean by that.

4:46What we're gonna do here is we're just going

4:47to define another class here called Developer,

4:52very similar to the example that I gave

4:53at the beginning of this section,

4:55and we're gonna make this class extend the Person class.

4:59So in other words,

5:00we're gonna make this class a subclass of the Person class.

5:04And in order to do that in Python,

5:06the syntax looks like this.

5:08We just put parentheses after the subclass' name,

5:12and we tell it what we want the superclass to be.

5:15So in our case, that would be Person.

5:16And by the way,

5:17this is something that we'll cover in much more detail

5:19in another section, but I just wanted to show you here

5:23so that we could go into a little more detail

5:25on this isinstance function and some of the caveats of it.

5:29So we're just gonna say pass, right?

5:31We're not gonna worry

5:32about implementing this subclass right now

5:34because that's really not the point here.

5:36But if we now create a developer, right?

5:39So let's say that we change person_1 here to a Developer.

5:43Well, you might be surprised to find

5:45that this isinstance function is still gonna return true

5:49for person_1 when we ask it

5:51if it's an instance of the Person class, right?

5:54Because even though person_1 is now directly an instance

5:58of the Developer class,

6:00it's also considered to be an instance of the Person class.

6:03So in other words, right,

6:05if we draw out our little class tree here,

6:08all right, so we have Person up at the top,

6:11and we're extending Person

6:14when we create our Developer class,

6:17and then we're creating an instance of this Developer class,

6:19which we're calling person_1,

6:21but really it's just me, right?

6:22So I'll just write Shaun here.

6:24So just to be clear,

6:25when we ask Python if Shaun here is an instance

6:29of the Developer class,

6:30well, it's going to return true, right?

6:32Because it's a direct instance of that class.

6:36And you know, when we created person_1 here,

6:39right, when we created this instance,

6:41we actually used the Developer constructor to do that.

6:46However, it's also considered

6:47to be an instance of this Person class

6:49because the Developer class is a subclass

6:52of that Person class.

6:53So that might get a little bit confusing here.

6:56Really, all you need to know is

6:57that isinstance just asks whether this class is somewhere

7:02in this object's class hierarchy, right?

7:06It's not asking whether it's a direct instance

7:09of that class.

7:10So anyway, that's the isinstance function.

7:13And before we move on,

7:14there's one other function that I wanna talk about,

7:16which is actually going to be very convenient

7:19to demonstrate now that we have this Developer subclass,

7:23and that is a function called issubclass.

7:27Now this one is basically just used

7:30to tell whether one class is a subclass

7:33either directly or indirectly, right?

7:36If there's a class or two in between those two classes

7:39in the hierarchy of some other class.

7:42All right, so in our case here,

7:43Developer would be a subclass of this Person class,

7:46and we can test that out by,

7:49we'll just print it out to the console,

7:50I guess to make it easy for ourselves.

7:52We'll just say, "Developer is a subclass

7:57of Person," and then we'll print out the value

8:02that the issubclass function returns, all right?

8:05So let's just fill in Developer, right?

8:08The first argument is going to be the class

8:10that we want to check,

8:12and the second argument is going to be the class

8:16that we want to tell if the first argument is a subclass of.

8:20So let's just run our code here, and sure enough,

8:23we'll see that it says,

8:24"Developer is a subclass of Person: True."

8:27And if we flip this around, right,

8:29if we ask if let's say Person is a subclass of Developer,

8:34well, we're gonna see that it now returns false.

8:38Okay, and again, another thing to keep in mind here is

8:41if we were to have a slightly larger class hierarchy,

8:45so if we were to have a class

8:46that extends the Developer class,

8:49we'll call this something like PythonDeveloper,

8:53and this is going to extend the Developer class,

8:57and we'll just put pass inside of that class

8:59because we don't want to implement anything just yet.

9:02Well, if we ask Python

9:03if the PythonDeveloper class is a subclass

9:07of the Person class, right?

9:10And that's what the syntax for that would be,

9:13let's just try this here.

9:15Sure enough, we're gonna see that that returns true.

9:17And by the way, I haven't been updating this message here.

9:20The main thing that you'll wanna pay attention

9:22to is the true or false value here.

9:25All right, so the point is that just like

9:26with our isinstance function,

9:28how it would return true even

9:31if an object wasn't a direct instance of a class,

9:36the issubclass function is going to return true even

9:39when a class is further down in the class hierarchy.

9:43All right, so this doesn't mean

9:44that PythonDeveloper directly extends the Person class,

9:48but it does mean that the Person class is somewhere

9:51in the PythonDeveloper class' ancestry or hierarchy,

9:56if you want to call it that.

9:57So anyway, those are the isinstance

10:00and issubclass functions.

10:02I hope this has been informative for you,

10:04and I'd like to thank you for viewing.

Challenge: Build a Library Management System

0:03<v Instructor>All right.</v>

0:04So now that we've learned all about

0:06the basic object-oriented terms, concepts,

0:09and syntax in Python, it's time for you to prove it

0:12to yourself by solving a challenge.

0:15Now, the way that this is gonna work

0:17is I'm going to give you a simple challenge

0:20that will incorporate all of the concepts

0:22that we've talked about in this section.

0:24And I recommend that you take probably anywhere from five

0:28to 10 minutes to try and solve the challenge,

0:30and once you've given it a good try,

0:33you can go on to my solution walkthrough video,

0:36where I'll actually show you how I went about

0:39solving this challenge myself.

0:42Okay, so the basic challenge is going to be this.

0:45You're gonna use the concepts

0:46that we learned about in this section

0:48to create a library management system.

0:52And first of all, when I say library,

0:54I'm talking about the kind with books in it,

0:56not the kind of library

0:57that you install into your code base.

1:00And to give you a few more details about this challenge,

1:03you're gonna be creating two main classes.

1:06The first one is going to be a library class,

1:09which will represent the entity

1:12that is in charge of managing books, right?

1:15And the second class is going to be a book class.

1:18Now, here's your task.

1:20You're gonna create these classes

1:22and they're gonna have to have the following criteria.

1:25The book class is gonna have to have a few attributes

1:29that books normally have.

1:30So it's gonna have a title attribute, of course,

1:33an author attribute, as well as an attribute

1:36called something like number of pages.

1:40Okay, and it's really up to you if you want

1:41to add extra attributes if they make sense to you,

1:44but with this book class, the other thing

1:46that's gonna be important for you to do is allow yourself

1:49to specify an initial value for each of these attributes

1:52through the books in knit method.

1:54All right?

1:55So in other words, you should be able to specify the title,

1:57author and number of pages when you create a new book.

2:01So that's the book class that you're gonna be creating.

2:04Over here on the library side of things,

2:07the library class is going to be in charge

2:10of managing books.

2:11And what this means is that it's going to have an attribute,

2:15which will be called something like books, right?

2:18And this will just be a list

2:20or whatever other iterable you want to use

2:23of all of the books that are in the library.

2:25And additionally, the library is going to have four methods.

2:30The first method is gonna be a method called Add Book.

2:34And this is going to be the equivalent

2:36of the library buying a new book

2:38and putting it on their shelves.

2:40And additionally, it will also have a method called

2:43Remove Book, which will be the equivalent

2:46of a library deciding that a book

2:48is not in good condition anymore and getting rid of it.

2:52So we've got methods for adding

2:54and removing books from a library instance.

2:57And additionally,

2:58the library class should have two other methods

3:00for allowing library visitors to check books in

3:05and check books out.

3:06So what that means is your library class is also gonna have

3:10methods called Check-In, that of course,

3:13will take a book as an argument.

3:14And I haven't been writing those in these parentheses here.

3:17The parentheses are really just to show

3:18that these are methods,

3:20and you'll also have a method called Check-Out.

3:24Cool. So anyway, that's the basic task here.

3:26This should really help you solidify the main concepts

3:29that we covered in this section.

3:30So again, give this five to 10 minutes

3:32to write out your code,

3:33and once you're done, take a look at the solution video

3:36where I'll show you how I implemented all of this code.

3:40So I hope this has been informative for you,

3:42and I'd like to thank you for viewing.

Solution Walk-Through: Build a Library Management System

0:02<v Shaun>Alright, so hopefully you've given the challenge</v>

0:05at least an honest try and taken maybe 10 minutes or so

0:09to try and implement all of the things that we talked about.

0:12And now that you've done that,

0:14I'm gonna show you how I solved this challenge.

0:16So the first thing that I did obviously

0:18was I created a new file and I ended up calling this file

0:22library-mgmt.py.

0:25Okay, so once I was in here,

0:27I started off by just creating both classes

0:29just to get them in place.

0:31So I created the Book class

0:33and then I just wrote pass in there

0:35just to get the Book class written out.

0:38And then I created the Library class

0:40and did pass inside of there as well.

0:43So going back to the Book class,

0:45now that we've got both of these in place,

0:48the Book class, as we said, needed to have a few attributes

0:50and we also needed the ability

0:52to set all of those attributes through the init method.

0:56So what I did is I removed the pass keyword

0:59and I added the init method.

1:01And then as you hopefully remembered,

1:04the first parameter that the init method

1:06as well as any instance method in Python classes has to take

1:10is self, a reference to the current instance

1:14that we're dealing with here.

1:16And then we added title, author and number of pages,

1:21which I just abbreviated as num_pages.

1:24Cool, so inside this init method now,

1:26all I did was I took the title, author,

1:29and num_pages parameters, and I used those

1:32to set the initial values of the attributes.

1:36So we said self.title = title

1:40and then self.author = author

1:43and self.num_pages = num_pages.

1:48And that's pretty much all we need to do, right?

1:50I didn't print out something inside this init method

1:53because, well, that's generally not considered

1:55to be a good practice unless you need it

1:57for debugging or something like that.

1:59So now that I did that, I moved on to the Library class.

2:03And the Library class is a little bit interesting

2:06because as we said,

2:08it's really only going to have one attribute,

2:11and that is an attribute keeping track

2:13of all of the books that are in the library.

2:16So what I did for this is I just defined another init method

2:20inside this library, and then we started off with self

2:24and that was in fact the only parameter that I added

2:26to the Library classes init method.

2:29And then inside here I started off

2:31by creating a books attribute on the Library class

2:36or on instances of the Library class that is,

2:39and I set that to an empty list, right?

2:43So I just created a new empty list

2:45that we could later on add books to.

2:49And that's pretty much all I did

2:50in the init method for the library.

2:52So the next two methods that I defined were the two methods

2:56for adding or removing a book from the library.

2:59And this is more in the case of the library buying new books

3:03or throwing books out.

3:05So what I did is I started off

3:07by defining an add_book method.

3:10And of course since this is an instance method,

3:12we had to define self as its first parameter.

3:15And then the second parameter was book, right?

3:18That's the book that we wanted to add to the library.

3:21And all I did just to start off with

3:24was I said self.books.append

3:27and I appended that book onto the library's list of books,

3:32the books instance attribute of the library.

3:37And that's pretty much it for the add_book method.

3:40So I moved on after that to the remove_book method.

3:44And what I did for this is I just said self

3:46and I used the title of the book

3:49as sort of a unique identifier.

3:52Now this was just a design decision that I made.

3:55You could have just said book like so,

3:58and passed in an actual instance of the book.

4:01But I just used the title

4:03because of how I implemented the body of this book method.

4:07What I did is I said self.books

4:10and then I actually used a list comprehension

4:13to remove any books in the library with that title.

4:17So I said self.books =

4:19and don't worry if you need to dust off

4:21your list comprehension skills here.

4:23I'm just going to show you what I wrote.

4:25So I said book in Self.books

4:30if book.title

4:34is not equal to title.

4:36Alright, so basically I just got all of the books

4:38from self.books whose title did not match

4:42the title parameter here.

4:44And oops, I actually wrote this wrong.

4:46This should be book for book in self.books.

4:50Okay, so got a little bit thrown off

4:51by the list comprehension syntax.

4:54But anyway, that's what I have,

4:56and that's all I did for the remove_book methods.

5:00Alright, so the next two methods

5:02maybe threw you off a little bit

5:04because what they required us to do

5:06was think about how we wanna keep track

5:08of whether a book is currently checked out or not.

5:12So what I did here, and this is really just one option

5:15that you could have gone with, there are really other ways

5:18that you could have done this,

5:19but what I actually ended up doing

5:21was adding another instance attribute

5:24to the Book class by saying self

5:27and then I added an attribute called status

5:29that could either be available

5:32or something like checked out.

5:35So now the idea is that whenever we want to check a book in

5:38or check a book out, all we have to do

5:40is change that status attribute on the book.

5:44So here's what that would look like.

5:45We would just say define, and then we could say check_in.

5:51And this would of course take self as the first parameter.

5:54And I again just used the title of the book

5:58that we want to check in or out.

6:00And again, this is just a design decision.

6:02If you made it so that these methods actually took a book

6:05as the second parameter, that's just fine as well

6:08as long as you got it to work.

6:10So for this check-in method now,

6:12what we're gonna do is we're gonna start off

6:14by finding the book with this title.

6:16And the way that I did that was

6:18by using Python's built-in Next method.

6:21But you could have done this

6:22in just the same way with a for loop.

6:24So I said book = next

6:28and then I used a list comprehension.

6:30So I said book for book in self.books.

6:35So very similar to what I did

6:37in this remove_book method that I defined.

6:40And then I said if book.title is equal to title,

6:46and then I provided a backup value here of None,

6:50so that if there were no books that match that title,

6:52we wouldn't end up raising some sort of exception.

6:55So anyway, now that we found the book,

6:57the next thing that I did was I said,

6:59if book is not None,

7:02so basically if we found a matching book with that title,

7:06then what we did is we said book.status

7:10and we set that to available, right?

7:12Because this is the check_in method.

7:15And I didn't add an else clause to this if statement

7:18because that's really kind of beyond the scope

7:20of this challenge.

7:21But if you went ahead

7:23and did something like that to handle the case

7:25where that book isn't actually in the library,

7:27that's all right too.

7:29In fact, that's just great.

7:31Cool, so the last thing that we had to do

7:33was define a check_out method.

7:35And this one actually looked very similar

7:37to the check_in method.

7:39So I made this take a self parameter and a title parameter.

7:43Again, you might have done a book

7:45as the second parameter there.

7:47And I just did the same basic thing

7:49as in the check_in method for this checkout_method.

7:53I found the book and provided None as the backup value,

7:57but in this case I set the book status to checked_out,

8:03or you could have set this to unavailable or whatever.

8:05Basically just something indicating

8:07that this book is no longer available in the library.

8:10So anyway, that's pretty much all we had to do

8:13for creating the book and Library classes.

8:17If you went so far as to actually test

8:19these two classes that you created,

8:21which I would highly recommend as a rule of thumb,

8:24here's what that might look like.

8:26We would create a new library,

8:27we'll just call that something like my_library = Library.

8:31And this would just initialize

8:33the self.books attribute to an empty list.

8:37And then we could add books to this library

8:40by saying my_library.add_book.

8:44And what you could do here is you could either create a book

8:48separately by saying something like

8:50book_1 = Book

8:53and then filling out all of the different arguments there.

8:57But what you could also do

8:59is just create that new book in place.

9:02All right, so if you don't need a reference

9:04to this exact book later on in the program,

9:06this would be the way that you could go about doing that.

9:09And here what I did is I just said title.

9:11So we could have something like "Pride and Prejudice,"

9:15and this is by Jane Austen.

9:19And let's just say that this has something like 279 pages.

9:24Alright, so that would add a book to our library,

9:26and let's just add a few more books here

9:29for the sake of demonstration.

9:31So the next book could be something like

9:33"War and Peace" by Leo Tolstoy.

9:39And this is a very long book,

9:41something on the order of like 1400 pages, let's say.

9:45And for our last book here, let's say "The Great Gatsby,"

9:49and this is by F. Scott Fitzgerald.

9:52So we'll type that out here.

9:54You might have used slightly less classy books here,

9:56but that's okay too.

9:58And for this one, let's say something like 150 pages.

10:03Cool, so now that we've added books to our library,

10:05the next thing that we can do is try checking out a book.

10:09So that would look something like this.

10:11We could say something like my_library.check_out

10:16and we'll try checking out "Pride and Prejudice" here.

10:19So we'll say "Pride and Prejudice,"

10:22because again, the way that I implemented the Library class

10:25was we had to actually use the title as the unique ID.

10:28And if I were to go back and do it again,

10:30I might not do it that way because there is a chance

10:32that two or more books have the same title.

10:34But anyway, that'll be fine for now.

10:36So that would check out the "Pride and Prejudice" book.

10:39And in fact, in order to verify

10:42that this is actually doing what it's supposed to be doing,

10:45what we're gonna do is we're going to define

10:48an actual variable to hold this.

10:50We'll just say book_1 =

10:52and oops, I didn't mean to do that.

10:53We'll try that again, book_1 =

10:55and then I'll paste that there, there we go.

10:58And now we can say add_book(book_1)

11:00And after we check out "Pride and Prejudice,"

11:03let's just print out the status of "Pride and Prejudice."

11:07So we'll say status,

11:09and then we'll change this to a template string.

11:12So we'll say book_1.status.

11:16Cool, so at this point,

11:17let's try saving and running our code.

11:19So we're just gonna say python3 library-mgmt.py

11:24and sure enough, we see that the status is,

11:26oops, it looks like I wrote checkout-out.

11:28I'm not sure why I did that.

11:30That should be checked_out.

11:32So let's just try that one more time

11:34to make sure I fixed it, and sure enough,

11:35we see status checked_out.

11:37So let's check that book back in by saying

11:40my_library.check_in

11:44and we'll check "Pride and Prejudice" back in, of course.

11:46So we'll say "Pride and Prejudice,"

11:50and then we'll print out the status of that book again.

11:52So sure enough, it's back in the library

11:55and its status is available.

11:57And last but not least,

11:58let's say that "Pride and Prejudice"

12:00has been checked out and checked back in

12:01so many times that its binding is now all tattered

12:04and the library wants to get rid of it.

12:06Well, in that case, we could just say my_library.remove_book

12:09and we would remove it by its title,

12:13"Pride and Prejudice" one more time.

12:16And that would remove it from the library.

12:19And if you wanted to see whether that was the case,

12:20you could always just say print

12:22and you could print out my library.books if you wanted to.

12:27And sure enough, we would see

12:28that there's only two books in there.

12:29So this is not exactly in human readable form,

12:31but at least we know that there are only two books

12:34in the library left, which would seem to suggest

12:37that we were successful in removing our books.

12:39So anyway, this is just how I went about

12:42solving this challenge.

12:43Hopefully this allowed you

12:44to stretch your brain a little bit

12:46and solidify the skills that we learned in this section.

12:49So I hope this has been informative for you,

12:51and I'd like to thank you for viewing.

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.

Request a Demo

Just need PCPP1? Enroll from $300/yr (58 skills)

Request a Demo