A constructor is special class method that is used to initialize objects. In Python, a constructor method uses the special function name __init__().
- [Announcer] An instance of a class is called an object. It's creating by calling the class itself as if it were a function. Here in Komodo, I've opened a working copy of constructor.py from chapter nine of the exercise files, and, I'm going to close this bottom pane for a moment, and if we come down here to the bottom, you see down here in main, I create two objects, a0 and a1 from the animal class. And I'm initializing it with various parameters. Type of the animal, the name of the animal, and the sound that it makes.
I'm also simply calling "print animal", so "print animal" is this function here that expects an animal object and prints the animal. And I'm calling it directly here from the constructor. From the class, without creating an intermediary object. This works exactly the same way because if you'll remember, function parameters work exactly like assignments in Python. And so when I run this, you see I get three animals, a kitten named Fluffy that says "rwar", and if we scroll down here you can see that.
A duck named Donald that says "quack", and a velociraptor named Veronica that says "hello". So let's take a look at the class constructor. There's a special class method name called init, with double underscores before and after, so those are two underscore characters. One and two, both before and after the word init. And that's a special name for a class function which operates as an initializer, or a constructor.
And so you pass it three arguments, of course the first argument is always self, and that's what makes it a method, an object method, because this self points at the object. And then the three parameters type, name, and sound. And those are used to initialize object variables. And these are object variables because they're never initialized until after the object is defined, so they don't exist in the class without having been constructed into an object.
And you notice that the object variables all have an underscore at the beginning of the name. Again, this is traditional, and this discourages users of the object from accessing these variables directly. Instead, you have these accessors, or getters, I call them getters, some people call them accessors. Which simply return the value of those object variables. And then down here when we print the animal, we use those getters in order to access the variables.
So the object is created by using the class name as if it were a function name. And this calls the constructor. Now you'll notice that I had these three parameters, and I don't have an easy way of really remembering the order of them, so a lot of times instead, somebody will do it this way. We'll use "kwargs", and then each of these can be "kwargs" sub type, and name, and then down here, I can say, "type = 'kitten'", "name = 'fluffy'", and "sound = 'rwar'", and I'll go ahead and do this without the extra spaces around the equal sign.
And do the same thing here. And here, and now when I run it, of course, I have exactly the same result, but now it's a little bit easier to remember these parameters, and I can put them in in any order if I want to. In fact, I can even give them default values. I can say "if 'type' in kwargs else", and give it a default value.
And now, if I want to, I can come down here, and I can say "print animal", and just give it the animal class with simply parentheses, which will give it the default object. And now when I save and run it, you see, we get the last one here, which is a kitten named Fluffy that says "rwar". So that's just a little bit about the constructor. An object is an instance of a class.
An object is created by calling the class as if it were a function, and the constructor is used to initialize the object.
- 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