Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you're creating objects and classes in Python, you're going to want to get them some functionality. That functionality is usually created with methods. So let's go ahead and make a working copy of classes.py. We'll call this classes-working.py. We'll open up that working copy. You can see that we have a class called Duck and it already has couple of methods in it, quack and walk. Those get called down here. So donald gets created as class Duck. So this is an object with that class and the object donald.
We're calling the quack method on the object donald and we're calling the walk method on the object donald. The calls are done using this dot operator, which is used to reference an attribute of the object. In this case, the attribute is a method. So it's called with the parenthesis and that calls it as you would call a function. These are actually functions. It's just that these are functions that are attributes of an object. That's what makes the methods.
So we'll go ahead and we'll run this, and you'll see that the object gets created and the methods get called. It prints out Quaaack! and it prints out Walks like a duck. Everything is working like we expected it to. So methods are actually functions. You'll see that they're defined just like you define functions in Python. The difference is these are indented under the class Duck. So that makes them methods of the class Duck. You'll also notice that the first argument to these functions is this word self. That is a reference to the object. Not the class, but the object.
In other words, when quack gets called on the donald object, then the donald object gets passed to the quack method. So the quack has a way of referring to object attributes. You'll also notice that nothing is getting passed inside of these parentheses. It's as if donald was passed inside those parentheses, but you don't actually put that there. That happens automatically by virtue of the dot operator. When quack gets called as a method of donald, then donald gets passed magically inside those parentheses, so that it's the first argument to the method.
There's a special type of a method that I want to talk about which is called a constructor. This gets called every time you create an object based on this class. The constructor in Python is created by naming a method with two underscores and the word init and two more underscores at the end. This creates a constructor method. So I'm just going to print('constructor') here. And we'll go ahead and we'll save this and we'll run it. You'll see that constructor gets printed at the beginning.
So when we run this, the first thing we do is we create the donald object and that's where the constructor gets called. In fact, one of the most common purposes for a constructor is to initialize some data. So if I put in the number 47 here and then I pass in that value in the constructor, I'll say value here, instead of printing constructor here, I can save this value, saying self._v = value.
What this does is this creates a local variable that is an attribute of the object. In this case, the donald object, but if I had different objects with different names, it would be attributes of those objects. We'll see how valuable this is in a moment. Then I can use that value in the methods. For example, I can print it out here. I can print it out here. So now I'm initializing this variable to the number 47.
Each time I use it, I'm going to go ahead and print it out. So we'll save this and run it. Now we see that we have a 47 here and a 47 there. If I were to change this to something else, save it and run it. We have 52 here and 52 there. If I create a different object with a different number in it and go ahead and call it, now when I save that and run it, then we have the donald ducks and we have the frank ducks.
So the value here is that this self._v is actually attached to the object, not to the class. This is what's called encapsulation in object oriented programming, because that value is a part of the object. So I can have different objects with different values. I could actually have all kinds of different data associated with those objects. They could be opening different databases, they could be accessing different files, they could be keeping their place in these databases and in these files.
This is the power of encapsulation in object oriented programming. We'll see a lot of examples of that as we move forward, but for now what's important to know is that all this happens to this self variable right there and right there. When the object calls a method that self variable gets passed, and that's a reference to the object. All the things that are attached to the object, its other methods, its attributes, its data, is all carried there. So here we've defined methods in our object. These methods are defined in exactly the same way that we define a function in Python except that its first argument is always self and that argument doesn't get passed explicitly.
It's passed implicitly through the dot operator. Then we have a special method called init which is used as a constructor. We can use that to set up data. We can use it to open databases. We can use it for all kinds of purposes. The point of it is to initialize whatever needs to get initialized when you create the object based on this class. So that's how you can create a constructor and that's how you create methods in classes that will be used in objects in Python.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99586 Viewers
56 Video lessons · 112786 Viewers
71 Video lessons · 81606 Viewers
131 Video lessons · 39176 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.