Python 3's __init__(), self, Class and Instance Objects Explained Concisely

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Even though I already know Python I watched the whole thing, I didn't know that you could do this:

ClassName.method(instanceVar)

You learn a new thing every day

👍︎︎ 13 👤︎︎ u/reinaldo866 📅︎︎ Aug 29 2019 🗫︎ replies

This is amazing - so well explained man. Especially adding the passing the instance object in so that it essentially replaces self.

I think I finally get it!

👍︎︎ 3 👤︎︎ u/Not-the-best-name 📅︎︎ Aug 29 2019 🗫︎ replies

Nice and clear explanation. The next video could explain inheritance, super() and @class_method.

👍︎︎ 4 👤︎︎ u/edarchis 📅︎︎ Aug 30 2019 🗫︎ replies

Thanks OP, subbing to your channel. Still don't understand classes completley. But learntlittle more. My learning curve has stopped with functions.

👍︎︎ 2 👤︎︎ u/rjsh927 📅︎︎ Aug 30 2019 🗫︎ replies

well done

👍︎︎ 2 👤︎︎ u/thefreecat 📅︎︎ Aug 30 2019 🗫︎ replies

Nice explanation and video.

Quick question, I'm hearing the author say "dunder init" for the init method.

Am I losing my hearing?

👍︎︎ 2 👤︎︎ u/UncleTerwilliger 📅︎︎ Aug 30 2019 🗫︎ replies

Novice user here, thank you for the video.

I apologize for the somewhat elementary question, as I'm a new user struggling to conceptualize __init__(self) and it's utility.

The closest I can come understand the above is by analogy - Anaconda's v-env functionality. Within this I'm able to switch between envs and their associated kernels.

Would classes be like mini 'envs/kernels' within a single script?

Thanks again for the video.

👍︎︎ 2 👤︎︎ u/polandtown 📅︎︎ Aug 29 2019 🗫︎ replies

i hate this part of python so much

👍︎︎ 2 👤︎︎ u/thefreecat 📅︎︎ Aug 30 2019 🗫︎ replies
Captions
on viewer request let's talk about the init method and pythons self this is the only tutorial where I would urge that you follow along from start to finish this can be thought of as a single example only where each step follows on from the last here we have the most basic of class definitions with an empty body the result of this code is to create a class object and assign it a name the name of this class object is tweet you can think of it like a factory providing default behavior and able to create objects in its image and which does so whenever we execute the class name tweet followed by a pair of brackets this is known as calling the class the objects created in this way are known as instance objects the convention is for instance objects to start with a lowercase character and for class objects to begin with an uppercase character instance objects inherits any class attributes of which there are none here and they get their own namespace that means that if you want to access any attributes of that instance object for example message you can't just write message you need to write the instance name a and then a dot and then message that delineates the attributes that belong to the instance a and those that belong elsewhere we've assigned a string to the instance attribute message and we access it just in the same way as we assigned to it so if we now try to print the message attribute in the tweet namespace we'll get an attribute error because like we said earlier it's as if the class object is a factory that provides default behavior and when we create instance objects we're getting concrete items that we can change and any changes that we make are not then propagated back up to the factory they stay with the instance and die with the instance we can create as many of these instance objects as we like so here we've created another instance object and assigned it to B we assign a different string to e dot message and can demonstrate that the two message attributes are different exist at the same time and are accessed through their respective namespace methods that begin with double underscores and which end in double underscores our special hooks in Python these methods are also known as dunder methods dunder being short for double underscore these methods are called automatically at certain times classes can override most of these and we'll focus on the init method whenever we call class objects the instance object is first created with the dunder new method and then any attributes are initialized with the dunder init method so the dunder init method is best known as the initializer method although most people call it the constructor method it's called automatically whenever an instance is created so here we've redefined the tweet class we've overridden V dunder init method and we'll have it prints hi now when we go to create an instance we get an error and despite the fact that we didn't seemingly include any positional arguments when calling tweet the error is telling us that dunder init as we've defined it takes no positional arguments but that we provided one what's going on if we try to create an instance and we seemingly do include one positional argument the error says that we've given two well you might have spotted this error but defining dunder init without including any parameters is not what you normally see you would normally see self as the first parameter when we redefine the class to include self and we then create an instance our code works and we see hi whenever the class object is called the instance is always passed as the first argument but the init method isn't for printing high to term it's there to perform any initialization and make assignments to instance attributes whenever you see the word self in a class definition self always refers to the particular instance so now when we come to create our instance we see that we're missing an argument the message argument we've learnt that the instance is passed automatically as the first argument to in it whenever we're trying to create an instance and any other arguments that we pass in here will take up positions 2 3 4 etc now when we pass in a string when creating an instance a it works absolutely fine and we've printed that attribute here as confirmation we've created a separate instance of the same class passed in a different string and that works fine to both of those instances coexists in the previous class definition we had the instance attributes a different name to the parameter name just to highlight the fact that they were different but what you will normally see is something along the lines of this second class definition to reinforce the point if we don't include self as the first parameter in any method definitions in our class then those methods will only be available through the class namespace they won't have access to any instance attributes and just to clear up how this instance is magically making its way in as the first argument let's take a look at this we have instances a and B which each have their respective message instance attributes as being one and two so far so good there's nothing new here how about when we try to call the print tweet method but in the class objects namespace when we call this method this way it tells us that we're missing V self positional arguments now we said earlier that self is referring to the instance well we have two instances a and B when we pass in a we get A's message and when we pass in E we get B's message the what's happening when it looks like we're magically passing in the instant subject I hope that this is cleared up self and the dunder init method for you leave a comment below letting me know what you thought of this explanation and if you're on reddit and you thought this explanation was any good then I'd be grateful if you could give it an upvote to help give it visibility to others who may benefit from it also
Info
Channel: Live Python
Views: 73,173
Rating: 4.5799522 out of 5
Keywords: python self init, python self explained, python init explained, Python OOP tutorial, python 3 OOP tutorial, python self, self in python, python tutorial, python 3 tutorial, python 3, python, python classes, classes in python, __init__ python, python class, python classes and objects, class in python, classes python, class python, python objects, python classes and objects tutorial, init method in python
Id: AsafkCAJpJ0
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Thu Aug 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.