On behalf of all Learning Python Programmers, We thank you for giving this freely accessible , clear and concise information on the subject. If I'm ever able to make money from programming (which I hope one day) I would like to help support you on patreon or something.
Learnt python from many sources but this concept of self, couldn't understand this better. Thanks for this amazing video, cleared my most of the doubts.
I'd like to thank you sir for being so generous in supplying us all with such a great presentation...You are blessing many people with this attitude ...Once again thank you from Brazil...
You have changed my life. Went from doing to understanding which is a much better place to be. Can' t say how much I appreciate this. Do you have any videos on the @classmethod?
İnanılmaz dərəcədə bəsit, başadüşülür şəkildə izah edir! Thank you very much John, the best explanation of self parameter so far. Especially using id function was very helpful to comprehend the topic. Best regards from Azerbaijan.
So if objects get their own copy of methods, and the methods are called using dot notation after the object name, then what's the point to pass the object as a parameter as the object already have it's copy of method and the object itself knows which object it is?
Every object is identified by its id number and this is passed to self. A programmer can identify the object by the name but the language needs the id number. Regards Phil
Thanks for the clearest (and scouse-tinged) explanation I have found so far. So just to check - the 'self' parameter tells that method "please execute the code on the object you're invoked on". Is that not already implied by definition? What would happen if 'self' was not included?
Thank you for the positive feedback it is appreciated. If self was not used then the method would belong to the class. This would mean that each instance would not have their own 'copy' of the method. It is best to regard the class as the template from which each instance (i.e. object is created) for each of these instances to have their own method and data attributes then self has to be used. My accent is a mixture of scouse, North Wales, Lancashire and Kent. So it was a good spot. Best wishes Phil
Thanks for all the great job sir! I hope you are ok. So, every method inside a class that I as a programmer will define and intend to "call" from an instance must have "self" as parameter. Am I right? If so, which type of methods don't need "self" as parameter in its definition?
You can design your class so that it (the class) has methods. This means you do not need to create an instance in order to use such methods. I never design my classes in such a way. I create classes that require the creation of an instance in order to use the instance methods. I regard the class as a factory for creating instances. Best wishes Phil
What's meaning of 'ID' here, is it a python internal numeric identifier assigned to the object or is it a memory pointer/address? If the object were too big, would the memory accommodate it with just a single address?
+Yongcui Mi Every variable is an object and every object has a unique identifier. In CPython the id is the address of the location of the object. Of course the object will take up many locations so the id will be the address of the first location of the object. Consider the following videos: czcams.com/video/gxTXgsDsifQ/video.html czcams.com/video/EnLAEE1C7X4/video.html Regards Phil
I think that I got it. User defined classes behave in a different manner than built in classes. Even though I store same attribute in two instance of a user defined class there will be created two objects. In a built in class if I store same attribute for two instances there will be created only one object. For user defined classes "self" keeps track of objects. I'm wrong?
+Tom Sawyer Hello Tom, From the perspective of the models I am using if you create n integers with different values, then you have n instances of the integer class (i.e. n objects) each of these objects will have the same methods but each object will have its ‘own copy’ of each method - remember everything in Python is an object. Let’s say you create your own class with a number of methods and one attribute (i.e. storage of data). Then you can create n instances of this class (i.e. n objects) and each of these objects exist in their own right and these also have their ‘own copy’ of each of the methods. This will become clearer when I have finished the series as I will be introducing the concepts across the series. I am developing the series to more closely match the UML artefacts that are used to design object oriented programs. How Python actually implements its internals does not (in my view) help with this. However, so long as you apply a strict approach to developing your code you can match Python exactly to the UML artefacts. Regards Phil
As a programmer you type in the code for the class. Elsewhere in the program instances of this class are created i.e. objects. Every object is independent. 'self' receives a unique identifier for each object created. In order for a variable to 'know' where they are (i.e. to 'know' which object they belong to) self and the dot are placed in front of the variable name. This way each variable is tied to a specific instance (object) of the class. Best wishes Phil
@@johnphilipjones ok sir thank you, I have a program here class cls: x=10 def __init__(self) self.x="This is String" obj=cls() print(obj.x) can you please explain why x which is in __init__ method is running here while calling x but not the x which has value 10..but 10 is printed when i remove the __init__ method
The programmer (i.e. us) do not provide the id that is passed to self. This is done automatically by the Python language. So each object created gets its own id which is provided by Python. Best wishes Phil
@@johnphilipjones sir ,If "self" gets object id,(okay by itself I'm not going into that)then why are we,as a programmer do object.method I mean this is redundant,if "which object's method?" Is decided by id provided to self,then why write object.method
'self'' is provided by Python it identifies the instance of the class (i.e. the object). A class is a template (or factory) from which objects are 'created'. So if a class has 4 methods each object of the class has these 4 methods but they have their own copy of each. So 'self.method_name()' will invoke the method_name() of the object identified by 'self' Best wishes Phil
Ive already watched more than 10 videos about self, but I dont understand why this variable exist, why is it useful for. For now, for me, It only seems a kind of variable for complication sake only.
Think of it this way. A chef makes some scones and uses a cutter to cut out identical shaped scones. The scones are then cooked and they all look identical. What if every scone was given a different flavour through the addition of a food additive. How would the chef know which scone was which? The answer is the chef would not know by looking at them. He could have stamped a code on each scone before cooking and have a different code for each scone to reflect the flavour. A class is like the scone cutter, the objects of a class are instances - like the scones are instances of the receipe. So how are each object of the class identified? In code they will all have their own name. For the object to carry out its method it has to look at the definition of the method in the class definition. Now the class needs to know what object it is dealing with so when created every object is given a code. Where is this code stored? The answer is it is stored in self and every object will have its own code stored in a version of the variable self. If you did not have self where would you store the code? Best wishes Phil
On behalf of all Learning Python Programmers, We thank you for giving this freely accessible , clear and concise information on the subject. If I'm ever able to make money from programming (which I hope one day) I would like to help support you on patreon or something.
I totally agree
fantastic explanation, it's mad the way others can't explain this concept without confusing you more, but this is a great explanation, thank you
Learnt python from many sources but this concept of self, couldn't understand this better. Thanks for this amazing video, cleared my most of the doubts.
I'd like to thank you sir for being so generous in supplying us all with such a great presentation...You are blessing many people with this attitude ...Once again thank you from Brazil...
extraordinary explanation...deep into concept and exactly in a way that people must understand the concept!
Again, clearly explained as well as any boot camp one would pay for.
Best explanation of this topic anywhere. Thank you.
You're very welcome.
Best wishes
Phil
You have changed my life. Went from doing to understanding which is a much better place to be. Can' t say how much I appreciate this. Do you have any videos on the @classmethod?
Thank you for the positive comments glad the videos are helping. I have not yet covered @classmethods but they are in the pipeline.
Best wishes
Phil
I finally understood it!! These animations and drawings really really helped
most clear and in-depth explanation, thank you very much!!!
İnanılmaz dərəcədə bəsit, başadüşülür şəkildə izah edir! Thank you very much John, the best explanation of self parameter so far. Especially using id function was very helpful to comprehend the topic. Best regards from Azerbaijan.
Thank you for your positive comments.
Best wishes
Phil
Thank you for your great videos, Sir. Very helpful in both content and format.
Glad the videos are helping and thank you for the positive feedback it is appreciated.
Best wishes
Phil
Thank you
Simply amazing! Thank you very much for all those classes! Keep going please! :D
thats an amazing explanation ♥️♥️♥️♥️
mate u from the north of england, your video is mint
Yes from the north but now living in Kent. Glad you like the videos. Best wishes
Phil
So if objects get their own copy of methods, and the methods are called using dot notation after the object name, then what's the point to pass the object as a parameter as the object already have it's copy of method and the object itself knows which object it is?
Every object is identified by its id number and this is passed to self. A programmer can identify the object by the name but the language needs the id number.
Regards
Phil
Thanks for the clearest (and scouse-tinged) explanation I have found so far. So just to check - the 'self' parameter tells that method "please execute the code on the object you're invoked on". Is that not already implied by definition? What would happen if 'self' was not included?
Thank you for the positive feedback it is appreciated. If self was not used then the method would belong to the class. This would mean that each instance would not have their own 'copy' of the method. It is best to regard the class as the template from which each instance (i.e. object is created) for each of these instances to have their own method and data attributes then self has to be used.
My accent is a mixture of scouse, North Wales, Lancashire and Kent. So it was a good spot.
Best wishes Phil
Thanks for all the great job sir! I hope you are ok. So, every method inside a class that I as a programmer will define and intend to "call" from an instance must have "self" as parameter. Am I right? If so, which type of methods don't need "self" as parameter in its definition?
You can design your class so that it (the class) has methods. This means you do not need to create an instance in order to use such methods. I never design my classes in such a way. I create classes that require the creation of an instance in order to use the instance methods. I regard the class as a factory for creating instances.
Best wishes
Phil
What's meaning of 'ID' here, is it a python internal numeric identifier assigned to the object or is it a memory pointer/address? If the object were too big, would the memory accommodate it with just a single address?
+Yongcui Mi Every variable is an object and every object has a unique identifier. In CPython the id is the address of the location of the object. Of course the object will take up many locations so the id will be the address of the first location of the object. Consider the following videos:
czcams.com/video/gxTXgsDsifQ/video.html
czcams.com/video/EnLAEE1C7X4/video.html
Regards
Phil
I think that I got it. User defined classes behave in a different manner than built in classes.
Even though I store same attribute in two instance of a user defined class there will be created two objects. In a built in class if I store same attribute for two instances there will be created only one object.
For user defined classes "self" keeps track of objects.
I'm wrong?
+Tom Sawyer Hello Tom,
From the perspective of the models I am using if you create n integers with different values, then you have n instances of the integer class (i.e. n objects) each of these objects will have the same methods but each object will have its ‘own copy’ of each method - remember everything in Python is an object.
Let’s say you create your own class with a number of methods and one attribute (i.e. storage of data). Then you can create n instances of this class (i.e. n objects) and each of these objects exist in their own right and these also have their ‘own copy’ of each of the methods.
This will become clearer when I have finished the series as I will be introducing the concepts across the series. I am developing the series to more closely match the UML artefacts that are used to design object oriented programs. How Python actually implements its internals does not (in my view) help with this. However, so long as you apply a strict approach to developing your code you can match Python exactly to the UML artefacts.
Regards
Phil
I have one question sir, if self is just pass for instance of class than why its written before every variable that we create in method? Plz tell
As a programmer you type in the code for the class. Elsewhere in the program instances of this class are created i.e. objects. Every object is independent.
'self' receives a unique identifier for each object created. In order for a variable to 'know' where they are (i.e. to 'know' which object they belong to) self and the dot are placed in front of the variable name. This way each variable is tied to a specific instance (object) of the class.
Best wishes Phil
@@johnphilipjones ok sir thank you,
I have a program here
class cls:
x=10
def __init__(self)
self.x="This is String"
obj=cls()
print(obj.x)
can you please explain why x which is in __init__ method is running here while calling x but not the x which has value 10..but 10 is printed when i remove the __init__ method
Self is also implied with empty parameters right?
+Jurgen H Yes that is correct
Best wishes
Phil
What's the point writing firstobject.method , secondobject.method
If ultimately we've to provide the object id by ourselves.
The programmer (i.e. us) do not provide the id that is passed to self. This is done automatically by the Python language. So each object created gets its own id which is provided by Python.
Best wishes Phil
@@johnphilipjones sir ,If "self" gets object id,(okay by itself I'm not going into that)then why are we,as a programmer do object.method
I mean this is redundant,if "which object's method?" Is decided by id provided to self,then why write object.method
'self'' is provided by Python it identifies the instance of the class (i.e. the object). A class is a template (or factory) from which objects are 'created'. So if a class has 4 methods each object of the class has these 4 methods but they have their own copy of each. So 'self.method_name()' will invoke the method_name() of the object identified by 'self'
Best wishes
Phil
Ive already watched more than 10 videos about self, but I dont understand why this variable exist, why is it useful for. For now, for me, It only seems a kind of variable for complication sake only.
Think of it this way. A chef makes some scones and uses a cutter to cut out identical shaped scones. The scones are then cooked and they all look identical. What if every scone was given a different flavour through the addition of a food additive. How would the chef know which scone was which? The answer is the chef would not know by looking at them.
He could have stamped a code on each scone before cooking and have a different code for each scone to reflect the flavour.
A class is like the scone cutter, the objects of a class are instances - like the scones are instances of the receipe. So how are each object of the class identified? In code they will all have their own name. For the object to carry out its method it has to look at the definition of the method in the class definition. Now the class needs to know what object it is dealing with so when created every object is given a code. Where is this code stored? The answer is it is stored in self and every object will have its own code stored in a version of the variable self.
If you did not have self where would you store the code?
Best wishes
Phil