Class methods are functions contained in a class. There are rules for how methods interact with objects and data. Some capabilities require specially named methods.
- [Instructor] A function that is associated with a class is called a method. This provides the interface to the class and its objects. Here in Komodo, I've opened a working copy of methods.py from chapter nine of the exercise files. And if we look down here, we'll find this method called type which serves as both a getter and a setter, and so I call it a getter setter. You'll notice that the first argument to the function is self, and that's what makes this a method and not just a plain function.
This is filled in automatically, when I call this method on the object, I don't provide this argument. So I'll just provide one argument, and the fact that it's being called on the method will provide the first argument, self. It's common to name this argument self, although it's not required, you can name it whatever you'd like. But it's a really good idea to always use the word self, because it's traditional, and when people are reading your code, they'll know what it means. So in this case, the second argument is t, and you notice it has a default value of none, so if there is no value, or if it's none, then this if will fail and it'll just return the value of type.
If there is a value, then it'll go ahead and it'll set type, before returning it. So that's what makes it a setter getter. You'll also notice that the object variables are named with a leading underscore, and this again is traditional. Python doesn't have private variables, and so there's no way to actually prevent somebody from using these. But this indicates that it's a private variable and it should not be set or retrieved outside of the setter getter.
So when we run this, you'll notice that we have the same output as we did in previous lessons. And if I go down here to the bottom, you'll notice that I'm creating two objects, a0 and a1, for the two animals. And I'm printing them out. And we'll get, in a moment, to how this is happening, that I'm just printing them out with print directly like that. But if I take, say, a0 and I want to change its sound, I simply call the sound function and I change it to, say, a bark.
And now, when I run this, you'll notice that fluffy says bark, instead of rwar, like it was initialized to. And that's because I have set this variable, using the setter getter for sound, which is right there actually, at the bottom. Now you'll also notice this special method called str, with two underscores before and two underscores after. We've seen this before, in our constructor init, with two underscores before and two underscores after.
So this is a specially-named method, which provides the string representation of the object. And this allows us to print it with simply this print and the object like that, without needing a special function, like we had in the previous lessons. You can find a list of all the special method names here in the documentation, under data model. And if you click on special method names, you'll see a whole bunch of them, with all of their descriptions.
And there's str right there, the informal or nicely printable string representation of an object. And there's just a lot more of them. There's all the comparison operators, and this list goes on for quite a while. Methods are the primary interface for classes and objects. They work exactly like functions, except they are bound to the object through their first argument, commonly named self.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.