Pythons __init__ method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to have a look at patient's initialization method this diagram is an approximation to a UML class diagram and it tells us things about the class the first thing it tells us is that the class has this particular name here current account it also tells us that the class will have this particular attribute something that will in this case hold the name of the customer who has the current account and this represents behaviors now you can see I've used some terminology there where I call this an attribute and I call it behaviors but in the object orientated programming terminology you will often hear these things refer to differently for example you'll often hear this area here as referred to as data fields this is being referred to as methods or alternatively you can have this area being referred to as variables and this here as functions that are tied to a particular instance now to confuse matters if you look at some of the documentation that comes with Python it calls all of this area here attributes of the class and they will become attributes of the instances of the class but it goes on to say that this area are data attributes and this earlier our method attributes but I prefer to talk about this here as being attributes this here being behaviors and if you're doing analysis that's how you usually refer to these two areas this would be attributes and this here were behaviors but when we flip over to coding it is often the case that we call this earlier the variables and this area the methods that manipulate the data that's stored in the variables that are shown in a diagram here now to avoid the confusion of the different ways in which we talked about classes and instances of classes on what our methods and what our attributes and so on I personally prefer to think of things in a diagrammatic form rather than worry about the terminology I still use terminology obviously but the thing is I prefer to think of objects is looking as you can see here so we have the class as you can see based on UML class diagrams and this here is something I used to think of instances of that particular class in other words objects so if I was to say that this object is going to be based on this class then straight away I know that I'm going to have this method in the instance of the class and I'm showing that here as you can see and this method that's defined in the class I'm going to be showing that here so I have in the outer colored area the methods appealing but if I have a look at this self name which is going to be a variable and that's how I personally like to think of it as being a variable and attribute variable if you like or a data field remembered all of this terminology but with respect to the diagram I like to think of this as being in the center holding out an area which I'm showing as that white box into which data can be stored so this is my view of an object I also like to think of the object in this sense the reason I show this in the middle is I don't want that to be directly accessible to the outside world if I want to get at this variable then I have to write methods to get at it and what I mean by that you see this variable here will receive its value from this method and it's passed in here as a parameter if I actually want to allow the outside world to see what's stored in here I go and get that information via this particular method here so in other words although I have the variables in the center of this object I don't allow other objects to gain direct access to this valuable I arrange my code so you can only gain access to it via the methods that appear in the outside now I use this to try and highlight what is meant by encapsulation which is also kind of roughly related to data hiding in this sense I'm not interested really in how this object stores the data what I'm interested in as a programmer is how do I get at that data and what I'm saying I'm going to get at that data by either reading what's in it using this and setting its value using this method here let's consider this particular diagram again and we know that this is going to be the definition of a class from which instances are created in other words objects are created and of course it is possible to create more than one object and that's what I'm going to show here this is an object of this particular class and of course this object is going to have an instance method which is this initialization method here and it's also going to have the get customer name method which I'm showing here now at the center of this object we're also going to have this variable appearing as you can see here now this object is an instance in its own right these are examples of instance methods and this here is a variable that exists in this instance and this is an instance variable that may be become more clearer when we create another instance of the class as you can see here now this instance is going to have its own copy of the initialization it's going to have its own copy of the get customer name and at its center it's going to have its own variable which we can see is labeled self name and what I would like to stress here is these objects exist in their own right they just happen to have been based on the same class so this variable is a different variable to this one they exist in their own right within the objects that exist in their own right it's true to say that both of these have this variable in the center because the class actually told it is going to have a variable in the center but what we can see is that these variables exist separately so it's possible to have a name in here such as Rita Jones and have a name in here such as Philip Jones where they are the name of the customers that have their own current account object let's now move over to look at the computer program on which these diagrams have been based here you can see I've got the definition of a class which is called current account and here you can see I have got two definitions of methods now this one here is what this video is really about the preamble was to try and put the use of this in context now the first thing I want to look at is its name and you can see here there are two underscores and here there are two underscores now for want of a better word this is a special method in Python and the ones that have the underscores the double underscores we've seen before and these are ones that kind of come with Python but this is one that I can alter to my requirements for the kind of class I'm trying to produce here and what we can say is this is called the initialization method now when you create an instance of a class ie an object when that instance is created this particular method is called and it will set up some of the attributes of that instance now remember what we described a TGT has been before you'll often hear these called data fields but here I'm sticking with the word attributes or if you like this is going to set up the variables that our instance is going to have now this particular line here well that's a straightforward method and it's one I'll discuss in a moment it isn't one of the special ones and how do I know that well there's no two underscores at the beginning of it and there's no two underscores at the end let's now have a look at this particular class against this class diagram and it's what we've seen before you see this is the name which is taken from here this is the method which is this method this is the other method which is this method here and this is the attribute now where is that attribute declared in the class well you can see it appears here now that's quite important you can see that in this position I have got self name and this is where you set up these values here that appear in these diagrams inside this initialization method let's go through this program one line at a time well we come onto this particular line and you will have seen this in the previous videos this is what creates the instance of the class and of course we've seen this execution space before and what's going to happen is when this line executes we're going to get an object appearing in the execution space and you can see that this object has got this name and let's be careful here this name is bound to this instance why this name well it's the name that appeared here in the code and of course this instance we can see is based on the current account which is this class here so straight away I need to say well what does this particular object have what does this instance have well it has this initialization method it also has this get customer name method and in the center we're going to say that it has the variable self name as we've covered already in this particular video now of course if we look carefully at this particular line what we can see is in quality we have the name Rita Jones now that is a parameter and that parameter is going to be passed to this particular method here this initialization method but before we talk about the string Rita Jones let's just remind you about this parameter here yourself now that is going to receive the ID of this particular instance so here we're going to get the ID of that particular instance now where does that get passed from well it gets passed implicitly in other words when you construct you know that the ID of the instance is passed to this particular parameter here now Rita Jones this value will be passed here to customer name which is the formal parameter waiting to receive this actual parameter of course this is holding the string Rita Jones and it is assigned to this variable here and of course this valuable is this one so if you keep your eye on this you can see Rita Jones appears here now why the use of the word self well remember self is the ID of this particular object unless it's the ID of this object then we know that this valuable is at the core of this particular object so the Rita Jones knows which of the objects to go to of course you might have more than one object but of course in this case we're dealing with this particular object and of course Rita Jones the string knows to go here because self was passed in here that we can see here now what happens we come unto this line now this line is a print statement so that string there when we look at the output will go to the output but here we can see that we actually have a message and we know it's a message because we can see we've got dot notation and on this side of the dot you can see we have the name that is bound to the instance of the object which is this object here and on this side we have the method that's going to be invoked in this particular object and the method that's going to be invoked is this one and of course you can see it takes the word self which is the ID of this object and if you look at this bracket here nothing else is passed so when we come up here we know that this is receiving self which is the ID of this object so what happens this returns self dot name so it knows it can get at the variable that was declared here that we show in the center of the diagram so what will happen now is the string Rita Jones will be returned to this position and of course this print will now print out the customer name is together with the Rita Jones that's been returned from the message so what we get out is this where that string actually appears here and the Rita Jones which was returned because the message went to the object we can see is output here so to summarize we can see that this initialization method is responsible for setting up the variables that the instances are going to use and in other words we can initialize the variables and in this particular case you can see here we have self dot name which we can represent as existing within the object in its center and that particular variable is an instance variable because it belongs to the instance and not the class and we know it's an instant variable because we're passing in self here and this is using self as we can see here and of course we are initializing because we're going to be passing in the customer name and that customer name is assigned to this particular variable and of course in this position here I can have more variables declared as we'll see when we move on to the next video in the playlist check out the supporting website for these videos and also consider subscribing to the YouTube channel and the Google+ circle that relates to these videos in addition why not follow me on Twitter as I issue a tweet every time I upload a new video
Info
Channel: John Philip Jones
Views: 34,276
Rating: 4.8867927 out of 5
Keywords: __init__, Pythons __init__ method, Pythons __init__
Id: qtkSoe0INOI
Channel Id: undefined
Length: 15min 26sec (926 seconds)
Published: Thu Mar 24 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.