Constructor New & Init Method

5 minutes
Share the link to this page
Copied
  Completed
A constructor is a special kind of method that Python calls when it instantiates an object using the definitions found in your class. Python relies on the constructor to perform tasks such as initializing (assigning values to) any instance variables that the object will need when it starts.

Transcript

Hello, in this video we are going to cover constructors and the init method within Python in relation to classes. So first of all, what are constructors in general within classes within the realm of program. So constructors is constructed the method that is called when the class is first created. It's a great way of setting up any content like variables, for example, and any other setup so that you'd be worried for and what you're seeing a long time or a lot of websites when it comes to the pipeline. Something called dear, it's usually good to actually have it you know, to have the constructor source of the score in it. underscore underscore, this is just the format for this method, selves the path itself, you can pass in extra variables, well, cola and here we could pass horses soon.

Inconsistent. Okay. Okay, sorry. Okay, I'll just move to my ID. So more than that. So in here, we could do anything that we wanted to like I said that this morning because I haven't got a body for the, this init method.

So if you don't have a body then just get rid of it. And the default one will be caught. People think that this is the constructor, but it actually is Because this is taking the class the instance itself in technically it already exists. So this is not the constructor constructor. This is I should put the indentation right here. I'm just going to score you score any points CLF, which is class.

The property the return object. Object dot underscore, underscore new underscore underscore CLS. And let me just fix here. And so we do return objects new. And we just put an object here like so. objects.

Close bracket. So this technically is the construct tab. For Python, you generally won't use this, you'll use this, which is called after the class have been created. But it has access to all these variables and methods. So he can, you know, use that if he needs to. So I'm going to comment this out.

And we won't actually be using this. But that's how you would use the constructor. And why don't do is put a colon there. So we don't have to why would you want to use it? It's called initially. So I've got print six of Yeah, I'm calling the constructor is technically in this quality initialization method.

That's what it is. So if I run this now, stop it running again. So we get six printed effort because every time we created a class, your call called the constructor But Daniel Cole, the initialisation method and in here, let's say we wanted to pass in a parameter called speed. So this will be the speed and authentic default to zero so don't have to pass one in. So what we could do is get rid of this. So if I did self dot speed of this softer speed refers to this variable, but when I find it, this will fall to the local variable.

So now let's and the earth No, okay, that's good. So for printer this beautiful color one and color two. M for car two, I'm going to pass in a parameter. If I run it, see what we get. So we get zero 89 589 to zero because call one speed, we use the default variable and 89 for the cause being called to because we actually passing oppression So that was a great way of initially setting up our variable b on what the default value was. We allowed the developer to provide a value.

And this is fine because we increase the cost per lead of one, but we did increase the cost per lead of two, so it is still at night. So that's a for constructors. And the init method you'll be using, mainly this one but you're definitely I'm stressing the word constructor. You'll be using construction a lot when it comes to classes in any language, trust me, it is one of the most important aspects, I would say underrated aspects of classes in object oriented program. So that's it for this video. Thanks for watching, and I'll see you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.