Practice Exams:

Python Institute PCAP – Modules, Packages and Object Oriented Programming in Python

  1. Revisiting the Difference between Methods and Functions

So to start with discussing object orientation, I want to make a distinction between functions and methods because I’ve used these terms so far in the course. And we’re going to build upon this and you’re going to understand why. What’s the difference between a function and a method? So let’s start with an example of a list, okay? And we’ll call it my list. And so this has numbers from three to four. Whatever this list, I can pop things from this list. So when I do my list, dot pop. When I do a dot, this brings up all of these methods.

And notice there’s this little m next to each of these. This represents a method. All right? So what’s a method? What’s the difference between a method and a function? Because when I invoke the pop method on this syntactically, it looks like a function is being invoked right? There are these open and closed parentheses. What differentiates a function from a method is that a method gets invoked on a given object. Now, this is an object in Python, everything is an object. You’re going to understand that much better in a little while. So this method is being invoked on this object and what is it going to do? It’s going to pop off one of the items and then you can print the result. You can print my list. And the list has been changed up to this point. So let’s run this and notice it’s. Now only three values in there. We pop the last value.

So this is a method. Now, if I was to invoke a function on my list, I can use the max function. Notice max is not being invoked on any object. I could just use this directly. I could just pass in a list right here, an arbitrary list of random numbers like that. And when I run this, it’s going to actually give me it’s going to return the maximum number in this list. So let’s actually save that into something called the max value. It doesn’t really matter what you name the variable. So I’m just going to print max value here so you can see that this max function is being invoked. So let’s run and there we go. 900. So max is not being invoked on a given object. This function exists on its own. Same thing with min or some of the other functions that we looked at, like type and STR and int. They don’t belong to any particular object. Meaning I can’t do MyList max, right? As a matter of fact, if I do a dot max, if I type in M, there is no max method here. And if I was to try to invoke this right off the bat, we get an error. Unresolved attribute reference max for class list doesn’t exist.

Now, notice it uses this keyword class. It says, this method max does not exist for the class list. Take a snapshot of this image in your head, this error, because it’s going to make a lot more sense when we get into classes. So that’s the key difference between a function and a method. Methods are invoked on objects. Functions are just invoked and you pass in arguments to them. They can be invoked anywhere in your program as long as they’re visible in the file in which you’re working with. And so far in this course, we’ve seen that when you define a function, it needs to be defined first. So let’s say if I do def my name, and then this function is supposed to just print my name, let’s say, all right, I can’t invoke this function before it’s been defined.

I can’t do my name right here first, whereas it’s being defined later in this file. Hover your mouse on this and say it’s saying unresolved reference. So you can only use code that has already been defined previously because everything is sequential in a computer program. Line by line code is run. So you can imagine if you have a really large program, you’d have a very, very massive, massive file.

And that’s no good. That’s no way to code. So thanks to object oriented programming, we can better organize our programs. That’s essentially what object oriented programming is. It’s a way of organizing the instructions you’re writing for your computer program so that those lines of code can be packed up, put away, and you can always utilize them for later. Right? Rather than dumping everything into one file. And we do this through objects. The list that we were working with previously when I was trying to invoke the pop method on that list. That was a method being invoked on that given object on the list. Object? And same thing. We looked at the strip function, I think in the previous lesson, right, where we have a string let’s say this string is hello there. And there’s a bunch of spaces. I could do Dot strip and it will eliminate those spaces. This string is an object. And then as soon as you do a dot after this object and you call a method on it. This is actually a method that is defined inside of the string class. If I, for example, dot pop. Notice Pop doesn’t exist.

If I do a dot? P. There’s no pop here because that method is not defined for a string. Let me actually do that. Let’s just invoke, Pop, and you’ll see. Look at the error. Let’s hover our mouse. Notice unresolved attribute reference pop for class STR. Notice again the keyword class. Where’s the class? The class is defined somewhere in the Python libraries. And that class does not have the method pop in it. But it does have strip, doesn’t it? So I’m moving really slowly, step by step so that you don’t miss a beat.

Because this is a tricky area for a lot of people to grasp when they’re new to objectoriented programming. If you already know object oriented programming. You can feel free to skip one or two lectures here in this section so that you can continue moving on. But for those of you that are new to this concept, I’m really going to move very slowly. So that’s all I want to leave you with in this lecture was for you to, first of all, revisit the topic of the difference between methods and functions. Because method is an object oriented concept, whereas functions could just be used in any file. That your code, like we’ve been doing so far in the course.

And another key takeaway from this that I want to emphasize, and I’m going to continue to emphasize the next couple of lectures is object oriented programming is a way of programming. It’s a way of organizing the code that you write so that it’s reusable. Okay? Reusability is really important in writing computer programs. And object oriented programming allows us to reuse code that we’ve already written rather than writing new functions over and over and over again in new files that we create. And another big thing in object oriented programming is you don’t write all of your code in one file, right? We can have a massive file with a bunch of lines of code and that’s no good.

That’s not a good design. Object oriented programming allows us to break things into chunks and put them away and utilize those instructions of code whenever we need to by pulling them in whenever we want, rather than trying to clump everything together into one big massive instruction sheet, right? That’s no good. And there’s all this jargon associated with object oriented programming. I introduced to you a term just a few moments ago. Class. Class. We’re going to talk more about what a class is. It’s a fundamental component of organizing.

  1. Classes and Objects

Programming is a way of programming. The developer uses this technique to better organize their code so that essentially it’s more reusable, meaning you write it once and you can use it on multiple occasions. And the idea is to encapsulate this code in a file and put it away. And then whenever you need to run the code, you bring in the file and you utilize the code that’s already be written. It could be written by you in the past, or it could be written by some other developer, but it’s just a way of organizing the code so that it’s reusable. That’s what Object Oriented Design’s underlying purpose is, is to make reusability of code easier. Now, it does a lot of other things, and there’s a lot of other principles that we’re going to learn about, such as polymorphism and encapsulation and inheritance. Those are scary words, but trust me, you’re going to master those concepts after we go over the next couple of lectures. And since objecting is a way of organizing our code, there’s a special keyword that’s used often to bundle up certain things, certain objects, so to speak.

Now, if you remember, we were using a list. So if you just type in List and you do a command or control, click it, it’ll take you to this code here, class List. Don’t worry about the arguments here, and don’t worry about what this says, but notice it has some of the append clear copy. There’s various methods here. Now, the actual code is not going to be visible in this editor because this editor hides away some of the scary details. But notice the basic structure. You’ve got a class keyword, and then you have the name of the class, and then you have all of these methods defined in it. So this is essentially a template of what a list out to be like. And this code has already been written by the Python contributors so that we don’t have to rewrite all of this ourselves when we would like to have the ability to utilize a collection of items. The List class takes care of that.

So we’re going to be creating our own classes, and you’re going to learn all the ways in which you can utilize a class. So I’ll start with defining a class here, and let’s just call this class Vehicle, and this class will be the template of what a vehicle is in our program. Okay, we define those details here based on the rules that we define in this class. The instructions that we write as part of this class, we are specifying in our program what a vehicle is. Now, if I try to run this code just like this, it’s not going to run because it’s in our notice. It says end of file because we need to have some code as part of this class. So if you’d like to get to the code later or have some other developer look at it later. You can add the pass keyword. And I showed you that you could do that with if else statements and other nested blocks. And now this will compile. It still does nothing, but we do have a valid class called vehicle. So let’s define the template of what a vehicle ought to be.

The way to do that is you define a special method, okay? And you’re using the def keyword as we’ve been using, and then you give the name of the method, a special name, and it’s called in it all right, this is important. This init method is important because in this init method, you’re going to specify how a vehicle ought to be created. This init is short for initialize. And so when you want to work with vehicles in your program, you’re of course going to have many, many vehicles. So the definition of what a vehicle is and how it’s supposed to be constructed or created or initialized, those instructions are going to be defined in this method. Okay? Now, it accepts an argument by default. It takes self as the first argument. Don’t worry about what this is just yet, we’ll get into that later. Now the second argument to this could be, for example, the body type of a vehicle, right? Every vehicle has a kind of body, whether it’s a sedan, a sports, a jeep, what have you. So I’ll just write body type here.

That’s the first argument or the second argument in this situation that we expect the user to pass when they’re trying to create a vehicle. And then to keep it simple, let’s also get the make of the vehicle. All right? So we get the body type and the make. So the body type could be a truck, a bus or a car or whatever and sedan. And the make could be Honda or something, honda or Toyota, hopefully you get the idea. And then after pressing enter going into this method, we specify the attributes of the vehicle here, okay? And so we’re only really curing about vehicles body and the vehicle’s make, right? So we can say that vehicle body is equal to the body type that’s passed in as the argument. And this is not complete yet. I’ll show you exactly how to complete this. And then the next thing we have is the make. So we can say vehicle make and pass in the make that the user is going to be passing to us. And you’re going to see this in action shortly. Now, I haven’t really talked about the self argument just yet and you’re going to see exactly why it’s needed, okay? So getting out of this method and out of this class. Now after you’ve come out of this code, we can move on to bigger and better things, right? So let’s try to use this class.

Let’s say if I was to create a class, all I would do is car one or name it anything. Let’s just say I want to create a variable called car one, and I want to assign it a new vehicle so I could use the vehicle class. And in the arguments, I can specify the body type and the make, just like we’ve defined up here. And so this is how a vehicle would be initialized. So the body type, let’s say, is a Jeep and the make is Toyota. So this is how you would attempt to initialize a vehicle. And we’re saving that instance, that vehicle that we just created in the car one variable. Now let’s say that we wanted to print the vehicle’s body or the make. I could do. Let’s try that. And you’re going to see an issue that comes up. If we do car one, dot. Notice we don’t have access to any body or make.

So if I do vehicle body, notice it doesn’t know what this is. No suggestions of your mouse here, unresolved attribute reference vehicle body or class. So I can’t even print the data that I’m trying to save here. Let’s run it. Notice this gives me an error. Vehicle object has no attribute vehicle body. So notice it says vehicle object. Now what is the object? The object is car one. Car one does not have a vehicle body, does it? Well, you might say, wait a minute, but it’s here. It’s in this in its method. And this method is used to initialize a new vehicle, right? So why isn’t it being shown in car one? Well, the reason is we need to actually use self. So if we do self dot here, now all of a sudden vehicle body is exposed. Okay? And you can check that. If you do car one dot, you’ll see that there’s vehicle body as well as vehicle make. So let’s print the vehicle body. Let’s hit run. And the vehicle body is Jeep. And if we do vehicle make, you’ll see that it’s Toyota. Now that’s car one. Let’s create another car. I’m just going to copy this code and paste it down here again and change it to car two. Using the same template vehicle, I’m now going to create another kind of vehicle.

So let’s just say that this is a truck now. And the make of this truck is Mercedes. There you go, mercedes. Now, if I try to print the details of car two, I could do that right here. Print car two, vehicle make. Let’s run this. And notice Toyota is the first car object. The second car object is Mercedes. So both of these are the car one and car two are instances of a vehicle. Okay, where is the instructions defined of what a vehicle out to be? They’re right here in the vehicle class. So this self keyword was actually referring to the actual object. Now, what is the object? This right here car one. And car two is the other object. That is why if we get rid of this self here. This vehicle, body and vehicle make attributes are just floating in the air. They don’t belong to any given object. That is why we need this self keyword here. Very important. Now, technically speaking, this car one is just a variable and car two is just a variable. But it’s pointing to an object in memory. And we’re going to get into those details later. Don’t worry too much about what’s going on behind the scenes, but understand that this is an object variable, which means that it’s an instance of a vehicle. And same thing with this car two.

This is an object variable. It’s a different object of the same type vehicle. All right? So both car one and car two are a type of vehicle. As a matter of fact, let’s print the type for car one. And I showed you the type function, right? So we can do car one or car two or whatever. Let’s print the type for car one. Let’s run that. Notice the type is vehicle right here when you print that. Now if you do car two, notice the type is vehicle right here. And it also shows that it’s coming from a class, of course. So just like this, I can have many, many different types of cars, different objects. I’ll change this to car two, car three, car four, car five, car six. We’ve got six car objects, six vehicle objects. But it’s using the same template and the values could be different. So here I can have sedan for this one. I can have sport here, it could be a motorcycle or another sedan down here. And let’s say that this sedan is a Toyota and this sedan is a Honda. And the sport utility vehicle is a Mercedes. And the motorcycle is also a Mercedes. All right? So six car objects and we’re initializing them. We’re creating these objects here utilizing the vehicle class. And where are the instructions of what a vehicle is that’s in the vehicle?