I have been learning to program for 2 years, and until this video I had only grasped 50% of what "self" actually meant. I deeply appreciate all the tutors I have been learning under, however, this video shows that abstract concepts can be taught with minimal complexity and teaching isn't a skill everyone truly posses. Thank you, John.
FINALLY!!!!! I actually finally understand what self even means. Printing the id of self just solidified my understanding. YOU. ARE. AN. AMAZING. TEACHER. I'm going to look through your videos from now on when I need to learn a concept. Simply amazing!
Thank you for your generous comment. Please check out the website where videos are are organised into sections. www.PythonByteSize.com Best wishes Phil
I feel obligated to comment on this video due to its quality. I wasn't able to understand "self" before watching this video and I've been programming in and learning Python for almost a year. Moreover, I love the way this video is formatted and how this concept is explained. It's wonderfully easy to understand. I've liked and favorited this video and subscribed to the channel. Thanks for clearing this up for me!!
I think its funny, how there are other "Python Instructional Videos" that get like 500,000 views. and all they do is have some entitled, millenial, bafoons repeat words like "Awesome", " Excited", "Super Cool" during their mediocre explanations. Its like most people want to "feel" like they are learning, without actually learning. These Videos are phenomenal, its literally like having a real tutor, and is designed for people ACTUALLY wanting to learn. It's Well organized and laid out. These are the best
Thank you so much for going over self in details. Makes complete technical sense now that I see it like this. Codecademy didn't really explain it at all. You printing the ID also solidified it even more. This really helped me so much.
When compared to learning by reading a book and your videos, I can say these videos are awesome. The pictorial examples made it very easy to understand. Thanks a ton for the patience and effort in explaining the concepts so clearly.
You must have worked hard to put this together (and all the others lessons). Your work helped me a lot to understand how python works. I just want to say thank you from Italy
Thank you for the feedback it is appreciated. Check out the website at the following link: www.PythonByteSize.com I hope to visit Italy when this pandemic is over. Best wishes Phil
the id builtin looks super useful, and it makes sense that the self parameter in the method gets assigned the object ID derived from the builtin, thanks for a great lecture john!!
I have been stuck on this concept for days and all other explanations do little to nothing to help clarify this concept. You have made it easy as pie. Well done and keep up your high quality content.
Thank you for the positive feedback it is appreciated. The supporting website groups the videos see link below: www.PythonByteSize.com Best wishes Phil
Very good video. I have been struggling to understand the concept. It will take another watch or two, but it is the best explanation i have yet. massive thumbs up Thank you.
This is just another appreciation comment, I've become a huge fan of your teaching skills, the way you explain things is just phenomenal, sir I want to know whether you've the notes of the following videos, if yes then please let us know .
You can obtain the transcript of the videos on the same page as this video. Click the ... elipses beside the Save button and then just click 'Open Transcript.'
I am just clarifying my understanding. A class method exists in its own right. Though, when you create an object of that class, the method exists in its own right inside the object too. That's why when you define the class, you use self within the parameters, so that when you construct the object, it knows to use the method internally within that object that exists in its own right. Essentially using the ID number passed inside to it "self"?
The method is defined in the class with the self as you have described. This allows each instance of the class (i.e. object) to effectively have there own copy of the method. There is an issue with the language used to define class and instance methods which is best avoided. If you see a class that has methods declared (defined) using self then they are capable of ensuring each object effectively have their own copy. If self is not used then this will result in each object not being able to have their own copy. However, the method can still be executed by referencing the class name. But this type of coding should be avoided. Best wishes Phil
Why does every Python book have some kind of mangled explanation of this parameter, and yet none of them that I have run across really explain it well. It would seem than any object-oriented language would need to do this in some way, so would it be fair to say that Python just makes this explicit to remind the programmer what is happening? If so, is there any actual use for this parameter or tricks you can play with this parameter? What is the motivation for having that there? By the way, thanks. Very clear and step-by-step explanation.
+justgivemethetruthOther languages use something similar for example "this" and "me". Python uses it explicitly as it is possible for a function to be a function in a class or a method. If it is a method then you have to use "self" then this makes it a piece of code tied to the instance of the class. If you do not use "self" then you can message the class directly to invoke the method. However, I would not recommend accessing the class directly. Keep a class back for its main purpose that is as a template for creating objects. “Self” is used extensively within the class definition for example it is used to create variables that will be instance variables i.e. variables that “belong” to the instance (object) of the class. Regards Phil (John Philip Jones)
Great video and explanation. One question I have programmed in java and when u define a class in java it’s understood all the attributes and methods defined in the class will be a object one day. The class-type variable then is used to reference which particular object in a method call. So my question is, is python doing the same thing as java behind the scenes but requires the programmer to explicitly use the self parameter to tie to objects, why not just use the object names to reference the method? Long winded question From me my apologies but I like clarity ,because then I truly understand what’s happening. And this has been the first head scratcher I encountered in python like some others have said. Any response will help thank you!
Have a look at the following videos which address this issue. www.pythonbytesize.com/video-pythons-self-parameter.html www.pythonbytesize.com/video-python-method-parameters-and-self.html www.pythonbytesize.com/video-a-python-instance-method.html Best wishes Phil
tank you so much sir, lovely explanation. i am from java where it don't explicit mention about "self" keyword for method definition of class, is it hided from method definition and also why in python we have to explicit mention ? is there any advantage by doing so?
You can create more than one object from a class. Each object needs to be separately identifiable and self is used to achieve this i.e. each object has their own copy of self and each self contains a 'code' that is different in each case. This way each of the unique objects based on the same class can be identified. It is worth noting that self is not a reserved word. It is just standard practise to use self. You can choose another word and the program will still work - however do not do this always use self. Best wishes Phil
Wow! Wow Mr. JONES! You're the best! A very good teacher!!! I love all of your videos! Thank you very much! Could I please have your contacts if you don't mind! Thanks in advance!
This seems to clarify a lot. However what I don't understand is that on python 3 when I copy your code out and leave 'self' out of the code, it still works without the error you said you would receive. Why is that? This is my code class calculation: ## ## def two(): ## one = input('Type number 1: ') ## two = input('Type number 2: ') ## ## ans = int(one) + int(two) ## print(ans) ## ## def hello(): ## print('HI') ## ## ##clacultion = calculation() ##calculation.hello()
As you have not used self the functions in the class belong to the class. The last line of your code is messaging the class not an object (the name of object is a different spelling in your code) so the code works. As your code stands you are not following object oriented coding. I.e. you will not be able to make objects of the class that work. Best wishes Phil
Have a look at the following video: www.pythonbytesize.com/video-pythons-self-parameter.html and all the videos at the following playlist. www.pythonbytesize.com/classes-and-objects1.html The dot operator is referred to throughout this playlist on classes and objects. Best wishes Phil
Quick question, Phil. When you said that the demo_method within the class exists in its own right, AND my_object, the instantiated object based on the class, ALSO exists in its own right, do I understand correctly that now we have TWO independent instances of the demo_method, and have a copy of the demo_method within my_object?
That is a good question and one that engages my interest as I started off my career as a machine code programmer (I am an Electronic Engineer). However, it is not something I have investigated (yet). What I can say is every Python object has its own identifier that can be found by using the id() function. So, every instance of the class (i.e. object) is unique. Internally there may be some sharing of the code with each pointing to its own objects data. However, this is me just surmising. Looking at the issue from the perspective of a Software Engineer (I moved on from Electronic Engineering to Software Engineering) the objects are regarded as totally independent regardless as to how the compiler sets up the environment. To design an object-oriented software system would typically require the use of the Unified Modelling Language (UML). The artefacts of this language (at least the ones I use) show the software runtime as a community of objects communicating with each other via messages. You would typically use a collaboration or communication diagram to show this communication between objects. To fit in effectively with the design artifacts offered by UML it is important to regard objects as totally independent of each other. Many people will criticise object orientated programming because they do not view software systems as communicating objects. There are of course other methodologies for writing software that do not require an object-oriented approach. To fully appreciate the object-oriented development across the full software life cycle requires the belief (in my view) that objects are independent and together form a community were individual objects request services of each other by sending messages. Of course, Python supports numerous software development paradigms. This offers flexibility as a language but does not often help support a consistent development paradigm. I am not sure that this answers your original question, but it will hopefully give an insight into why I believe regarding objects as separate entities based on a class is more important. I hope this helps. Best wishes Phil (John Philip Jones)
@@johnphilipjones Thanks Phil. Very detailed answer. It's just that initially I interpreted that there WERE indeed two objects but now it seems you are reluctant to state that with certainty. I understand things in step-wise fashion. I work in the American 9-1-1 network industry, the equivalent of Britain's 999. The network is enormously complex, with mind-boggling detail. If there wasn't a way to understand things at a high level, in terms of call flows, it would be overwhelmingly, and chaotically senseless to the average learner. I adore the way you teach because it's step-wise at a logical level. Many people, when teaching, they skip steps. And an all too common thing in python CZcams videos, is that the tutor is flying, nay whizzing through the material and on top, is managing two separate files. Then suddenly, you are lost and you say, wait. Where is that? Where did he just go to? What's happened is that the tutor switched file without telling the audience. Someone that's in tune with their audience will say: "Now we will switch to file 2 and we'll create a method to call file 1" so we know where they are. I can't count the number of times I've had to slow down a video. Wait! What! It's exasperating.
Lastly, can you confirm I have this straight? When we put (self) in as a parameter to the demo_method(), when the code runs, my_object.demo_method messages my_object, then invokes the demo_method that runs inside the class. But how exactly do my_object and demo_method interact? What does that handshake look like, at a high level? Here is the way I'm reading what's happening: my_object passes self to the demo_method, and then demo_method says: "-Oh. I see you just passed your self to me (though invisibly and not explicitly in the code). As it turns out, I, demo_method also have one of those self IDs. I was able to read yours, and see that your self ID and my self ID are identical, so you belong to the same class that I'm part of and I can see that you want me to execute myself" (not in French Revolution style). Is that a good paraphrasing?
I consider the objects and self as follows: The class defines the attributes (data) and behaviours (methods) of any object that is based on the class. Let's assume we have a class called ClassA. Two instances of this class are created, by that, I mean two objects. Let's call these objects ObjectX and ObjectY. The class has a behaviour defined i.e method. This method is called DemoMethod(). This method has code to perform the required task of the method. In addition, the method has local variable to hold intermediate values of data produced during its execution. Methods are usually sent data to process and it is likely that this data will be different for the method ‘inside’ ObjectX and ‘inside’ ObjectY. Now ObjectX and ObjectY will both have the behaviour of the DemoMethod() as it was defined in ClassA. However, they have just received different data sent to their own copy of the DemoMethod(). Lets say that the DemoMethod() as it exists within ObjectX is executing and an interrupt occurs that has to be serviced. The DemoMethod() is forced to suspend its execution and the interrupting process is executed. Once the interrupting process has finished, execution is returned to the exact same place in the interrupted DemoMethod() so it can finish its execution. Now if ObjectX and ObjectY are in existence during runtime there will in effect be two DemoMethod()’s. However, these methods are from Pythons viewpoint different because they both have been identified by the id of the object to which they belong. So, each method (that is based on the same class) are unique and their uniqueness is achieved by them having the id of the object passed to self. I would therefore recommend that you regard the objects as being separate entities that have had their attributes and behaviour defined (or declared) by the class. However, for example, the attributes can hold different values for each of the objects. Both objects will have the same number of attributes as defined in the class, but these attributes can hold different values for each of the objects. For example, the details on a bank customer would typically be the name, address and bank balance etc. The details required for each customer will be defined in a class. Every customer of the bank will have themselves represented buy an instance of this class (that is an object). Every object would have their own value in the name attribute, their own value in the address attribute, and their own value in the bank balance attribute. So, in conclusion I recommend you regard objects as different entities existing in their own right. This will fit in well with the UML design techniques when developing software systems. However, I am unable to indicate how objects manifest at runtime with respect to the heap and stack mechanism because unfortunately I do not know. What I can say is that if you do not use self in the definitions of the method header the method cannot exist separately within each object created. Hope this helps. Best wishes Phil
why we need class concept in python. Instead we can use modules that can act as a class. for example in java we use class variable to get access to its field and methods. but in python we can use module name to access variable and methods inside that module. what the advantage of using class concept in python
A module groups functions and data structures into logical collections. If you used a data structure in a module it will be shared by all code within the module. You create instances of classes (i.e. objects) and each object can have its own copy of data structures. This is not possible with modules. Also, the object oriented development process is different to the procedural approach i.e. they are different paradigms. Consequently there is a different development process across the life-cycle for analysis, design and implementation.
why object is created outside class indention can we create instance inside class indention. forgive me for this silly question since i am from java we used to create instance inside class
That's how Python works. The Class is the blueprint for the object. So to create the object it must be done outside the class. I believe in Java, the objects are created inside the class, it's NOT like this in Python. . Objects are created OUTSIDE the class definition, unlike Java
I have been learning to program for 2 years, and until this video I had only grasped 50% of what "self" actually meant. I deeply appreciate all the tutors I have been learning under, however, this video shows that abstract concepts can be taught with minimal complexity and teaching isn't a skill everyone truly posses. Thank you, John.
Thank you for taking the time to give such positive feedback.
Best wishes
Phil
FINALLY!!!!! I actually finally understand what self even means. Printing the id of self just solidified my understanding. YOU. ARE. AN. AMAZING. TEACHER. I'm going to look through your videos from now on when I need to learn a concept. Simply amazing!
Thank you for your generous comment. Please check out the website where videos are are organised into sections.
www.PythonByteSize.com
Best wishes Phil
I feel obligated to comment on this video due to its quality. I wasn't able to understand "self" before watching this video and I've been programming in and learning Python for almost a year. Moreover, I love the way this video is formatted and how this concept is explained. It's wonderfully easy to understand. I've liked and favorited this video and subscribed to the channel. Thanks for clearing this up for me!!
same here, i struggled with 'self' but not until I found this tutorial.
I think its funny, how there are other "Python Instructional Videos" that get like 500,000 views. and all they do is have some entitled, millenial, bafoons repeat words like "Awesome", " Excited", "Super Cool" during their mediocre explanations. Its like most people want to "feel" like they are learning, without actually learning. These Videos are phenomenal, its literally like having a real tutor, and is designed for people ACTUALLY wanting to learn. It's Well organized and laid out. These are the best
Thank you the feedback is appreciated Best wishes Phil
The quality of the video, animation, editing and the python tutorial was way ahead of the time. Huge respect to you Phil!!
Thank you for the positive comments.
Best wishes Phil
The demonstration & the animated slides are so elegant and thus simple to understand. Thank you so much.
Thank you for the positive feedback it is appreciated.
Best wishes
Phil
(John Philip Jones)
Thank you so much for going over self in details. Makes complete technical sense now that I see it like this. Codecademy didn't really explain it at all.
You printing the ID also solidified it even more.
This really helped me so much.
Thank you for the positive feedback it is appreciated.
Best Wishes
Phil.
This is the best understanding of self/this and any other explination of OOP programing i've ever even heard of...
I've struggled with this particular concept since I started learning programming a few years ago. You just made it click. Thanks for the great video!
+ChillinAtDaClub Thank you for your generous comments. Glad they are helping.
Best wishes
Phil
(John Philip Jones)
You are becoming famous on the internet
When compared to learning by reading a book and your videos, I can say these videos are awesome. The pictorial examples made it very easy to understand. Thanks a ton for the patience and effort in explaining the concepts so clearly.
This is undoubtedly the best explanation I have seen so far!! Hat's off! Lots of love and respect for you!
Glad to hear that
Best wishes Phil
I am amazed by the clarity of this video. This has gotta be the best explanation on self. Thank you!
This one of the best explanations on the whole Internet. So clear and concise.
Best presentation about the "self" in python! your explanation is also very clear. Thanks !
Thank you for your videos, this is the first time I understand "self". A picture is worth a thousand words, and you have proven it video after video.
To be honest no one can taught like him ❤❤ thankYouSir
Best explanation of self! please keep on posting videos like this, great help for the beginners!
this series of videos are like the savior for those beginners, epic work!!!!!! man !
Thank you for the positive comments.Check out the supporting website at:
www.pythonbytesize.com/
Regards
Phil
(John Philip Jones)
You must have worked hard to put this together (and all the others lessons).
Your work helped me a lot to understand how python works.
I just want to say thank you from Italy
Thank you for the feedback it is appreciated. Check out the website at the following link: www.PythonByteSize.com
I hope to visit Italy when this pandemic is over.
Best wishes
Phil
Thank you very much for the tutorial. It is brilliant! I really like the way you 'hammer in' all the concepts and terminology again and again.
A teacher who knows in advance which questions can arise in the student's mind.... and removing that doubt also. Superb JPJ. Thanx
Thank you for your positive feedback.
Best wishes
Phil
It is good to know that the videos are helping. Thank you for your generous comments
Best wishes
Phil
the id builtin looks super useful, and it makes sense that the self parameter in the method gets assigned the object ID derived from the builtin, thanks for a great lecture john!!
I have been stuck on this concept for days and all other explanations do little to nothing to help clarify this concept. You have made it easy as pie. Well done and keep up your high quality content.
Thank you for the positive feedback it is appreciated. The supporting website groups the videos see link below:
www.PythonByteSize.com
Best wishes
Phil
So helpful... you definitely saved me here. Thanks so much!
Finally understood self with this video after n number of attempts with other sources..Thanks John
Best explanation I have come across. Very clear. Thank you.
I'm so lucky to find this tutorial video
Thank you sir. I truly appreciate your concise explanation of this topic.
You are very welcome
Best wishes
Phil
Thanks for the superb explanation. Made it really clear.
Great explanation!
keeep up the superb work - it rocks - you are the man!!!!!!
Really Thank you for your way and method tutoring python programing concepts, greetings with love
Very good video. I have been struggling to understand the concept. It will take another watch or two, but it is the best explanation i have yet.
massive thumbs up Thank you.
Beautiful explanation. Thank you !
Amazing Material !! Thank you very much :)
Brilliant explanation! Kudos on such a great job of simplifying things. The animation is worth the effort. Thank you!
Thank you for your positive feedback.
Best wishes Phil
Fantastic explanation!!!
Amazing video and very good to understand self :D
Best explanation ever
I just subscribed with a hope that all other videos have explanation as good as this 👌
awesome man... you rock thanks
Amazing !
Thanks John for the excellent video - hope you follow with more of the same.
This is just another appreciation comment, I've become a huge fan of your teaching skills, the way you explain things is just phenomenal, sir I want to know whether you've the notes of the following videos, if yes then please let us know .
I hope you collate the notes into a suitable format in the not to distant future.
Best wishes Phil
You can obtain the transcript of the videos on the same page as this video. Click the ... elipses beside the Save button and then just click 'Open Transcript.'
Dude you rock !
Sir u are the best make more videos on python these videos helped me a lot thanks a lot
And i subscribed
Thank You
Very clear :)
thank you
Thanks a lot
great
thanks
I am just clarifying my understanding.
A class method exists in its own right. Though, when you create an object of that class, the method exists in its own right inside the object too.
That's why when you define the class, you use self within the parameters, so that when you construct the object, it knows to use the method internally within that object that exists in its own right. Essentially using the ID number passed inside to it "self"?
The method is defined in the class with the self as you have described. This allows each instance of the class (i.e. object) to effectively have there own copy of the method.
There is an issue with the language used to define class and instance methods which is best avoided.
If you see a class that has methods declared (defined) using self then they are capable of ensuring each object effectively have their own copy.
If self is not used then this will result in each object not being able to have their own copy.
However, the method can still be executed by referencing the class name. But this type of coding should be avoided.
Best wishes Phil
So it's basically a replacement for static and not static in other languages? Excellent tutorial, a bit wordy though.
Why does every Python book have some kind of mangled explanation of this parameter, and yet none of them that I have run across really explain it well.
It would seem than any object-oriented language would need to do this in some way, so would it be fair to say that Python just makes this explicit to remind the programmer what is happening? If so, is there any actual use for this parameter or tricks you can play with this parameter? What is the motivation for having that there?
By the way, thanks. Very clear and step-by-step explanation.
+justgivemethetruthOther languages use something similar for example "this" and "me". Python uses it explicitly as it is possible for a function to be a function in a class or a method. If it is a method then you have to use "self" then this makes it a piece of code tied to the instance of the class. If you do not use "self" then you can message the class directly to invoke the method. However, I would not recommend accessing the class directly. Keep a class back for its main purpose that is as a template for creating objects.
“Self” is used extensively within the class definition for example it is used to create variables that will be instance variables i.e. variables that “belong” to the instance (object) of the class.
Regards
Phil
(John Philip Jones)
Great video and explanation. One question I have programmed in java and when u define a class in java it’s understood all the attributes and methods defined in the class will be a object one day. The class-type variable then is used to reference which particular object in a method call.
So my question is, is python doing the same thing as java behind the scenes but requires the programmer to explicitly use the self parameter to tie to objects, why not just use the object names to reference the method?
Long winded question From me my apologies but I like clarity ,because then I truly understand what’s happening. And this has been the first head scratcher I encountered in python like some others have said. Any response will help thank you!
Have a look at the following videos which address this issue.
www.pythonbytesize.com/video-pythons-self-parameter.html
www.pythonbytesize.com/video-python-method-parameters-and-self.html
www.pythonbytesize.com/video-a-python-instance-method.html
Best wishes
Phil
Hi really it is great explanation. is there any document is available for python tutorial . if there it will be helpful. thank you so much for.....
tank you so much sir, lovely explanation. i am from java where it don't explicit mention about "self" keyword for method definition of class, is it hided from method definition and also why in python we have to explicit mention ? is there any advantage by doing so?
You can create more than one object from a class. Each object needs to be separately identifiable and self is used to achieve this i.e. each object has their own copy of self and each self contains a 'code' that is different in each case. This way each of the unique objects based on the same class can be identified. It is worth noting that self is not a reserved word. It is just standard practise to use self. You can choose another word and the program will still work - however do not do this always use self.
Best wishes
Phil
thank you
TE AMO
Can you make tutorials of kivy, kivymd and kaki hotreload with multiple kv and py files? That will be an epic series.
I will put it into the pipeline.
Best wishes Phil
Wow! Wow Mr. JONES! You're the best! A very good teacher!!! I love all of your videos! Thank you very much!
Could I please have your contacts if you don't mind! Thanks in advance!
Can be contacted via the following link:
www.pythonbytesize.com/contact.html
Best wishes
Phil
Thanks a lot Mr JONES!!!
I try but I can not access to the site. The link does not work
Try the following link and then click on the 'Contact' menu item
www.pythonbytesize.com
Best wishes
Phil
This seems to clarify a lot. However what I don't understand is that on python 3 when I copy your code out and leave 'self' out of the code, it still works without the error you said you would receive. Why is that? This is my code
class calculation:
##
## def two():
## one = input('Type number 1: ')
## two = input('Type number 2: ')
##
## ans = int(one) + int(two)
## print(ans)
##
## def hello():
## print('HI')
##
##
##clacultion = calculation()
##calculation.hello()
As you have not used self the functions in the class belong to the class. The last line of your code is messaging the class not an object (the name of object is a different spelling in your code) so the code works. As your code stands you are not following object oriented coding. I.e. you will not be able to make objects of the class that work.
Best wishes
Phil
would you create a video tutorial that covers the dot-operator - that would be just great!!
Have a look at the following video:
www.pythonbytesize.com/video-pythons-self-parameter.html
and all the videos at the following playlist.
www.pythonbytesize.com/classes-and-objects1.html
The dot operator is referred to throughout this playlist on classes and objects.
Best wishes
Phil
Quick question, Phil. When you said that the demo_method within the class exists in its own right, AND my_object, the instantiated object based on the class, ALSO exists in its own right, do I understand correctly that now we have TWO independent instances of the demo_method, and have a copy of the demo_method within my_object?
That is a good question and one that engages my interest as I started off my career as a machine code programmer (I am an Electronic Engineer). However, it is not something I have investigated (yet).
What I can say is every Python object has its own identifier that can be found by using the id() function. So, every instance of the class (i.e. object) is unique. Internally there may be some sharing of the code with each pointing to its own objects data. However, this is me just surmising.
Looking at the issue from the perspective of a Software Engineer (I moved on from Electronic Engineering to Software Engineering) the objects are regarded as totally independent regardless as to how the compiler sets up the environment.
To design an object-oriented software system would typically require the use of the Unified Modelling Language (UML). The artefacts of this language (at least the ones I use) show the software runtime as a community of objects communicating with each other via messages. You would typically use a collaboration or communication diagram to show this communication between objects. To fit in effectively with the design artifacts offered by UML it is important to regard objects as totally independent of each other.
Many people will criticise object orientated programming because they do not view software systems as communicating objects.
There are of course other methodologies for writing software that do not require an object-oriented approach.
To fully appreciate the object-oriented development across the full software life cycle requires the belief (in my view) that objects are independent and together form a community were individual objects request services of each other by sending messages.
Of course, Python supports numerous software development paradigms. This offers flexibility as a language but does not often help support a consistent development paradigm.
I am not sure that this answers your original question, but it will hopefully give an insight into why I believe regarding objects as separate entities based on a class is more important.
I hope this helps.
Best wishes
Phil
(John Philip Jones)
@@johnphilipjones Thanks Phil. Very detailed answer. It's just that initially I interpreted that there WERE indeed two objects but now it seems you are reluctant to state that with certainty. I understand things in step-wise fashion. I work in the American 9-1-1 network industry, the equivalent of Britain's 999.
The network is enormously complex, with mind-boggling detail. If there wasn't a way to understand things at a high level, in terms of call flows, it would be overwhelmingly, and chaotically senseless to the average learner. I adore the way you teach because it's step-wise at a logical level.
Many people, when teaching, they skip steps. And an all too common thing in python CZcams videos, is that the tutor is flying, nay whizzing through the material and on top, is managing two separate files. Then suddenly, you are lost and you say, wait. Where is that? Where did he just go to?
What's happened is that the tutor switched file without telling the audience. Someone that's in tune with their audience will say: "Now we will switch to file 2 and we'll create a method to call file 1" so we know where they are. I can't count the number of times I've had to slow down a video. Wait! What! It's exasperating.
Lastly, can you confirm I have this straight? When we put (self) in as a parameter to the demo_method(), when the code runs, my_object.demo_method messages my_object, then invokes the demo_method that runs inside the class.
But how exactly do my_object and demo_method interact? What does that handshake look like, at a high level? Here is the way I'm reading what's happening:
my_object passes self to the demo_method, and then demo_method says:
"-Oh. I see you just passed your self to me (though invisibly and not explicitly in the code). As it turns out, I, demo_method also have one of those self IDs. I was able to read yours, and see that your self ID and my self ID are identical, so you belong to the same class that I'm part of and I can see that you want me to execute myself" (not in French Revolution style). Is that a good paraphrasing?
I consider the objects and self as follows:
The class defines the attributes (data) and behaviours (methods) of any object that is based on the class. Let's assume we have a class called ClassA. Two instances of this class are created, by that, I mean two objects. Let's call these objects ObjectX and ObjectY.
The class has a behaviour defined i.e method. This method is called DemoMethod(). This method has code to perform the required task of the method. In addition, the method has local variable to hold intermediate values of data produced during its execution. Methods are usually sent data to process and it is likely that this data will be different for the method ‘inside’ ObjectX and ‘inside’ ObjectY.
Now ObjectX and ObjectY will both have the behaviour of the DemoMethod() as it was defined in ClassA. However, they have just received different data sent to their own copy of the DemoMethod().
Lets say that the DemoMethod() as it exists within ObjectX is executing and an interrupt occurs that has to be serviced. The DemoMethod() is forced to suspend its execution and the interrupting process is executed. Once the interrupting process has finished, execution is returned to the exact same place in the interrupted DemoMethod() so it can finish its execution. Now if ObjectX and ObjectY are in existence during runtime there will in effect be two DemoMethod()’s. However, these methods are from Pythons viewpoint different because they both have been identified by the id of the object to which they belong. So, each method (that is based on the same class) are unique and their uniqueness is achieved by them having the id of the object passed to self.
I would therefore recommend that you regard the objects as being separate entities that have had their attributes and behaviour defined (or declared) by the class. However, for example, the attributes can hold different values for each of the objects. Both objects will have the same number of attributes as defined in the class, but these attributes can hold different values for each of the objects. For example, the details on a bank customer would typically be the name, address and bank balance etc. The details required for each customer will be defined in a class. Every customer of the bank will have themselves represented buy an instance of this class (that is an object). Every object would have their own value in the name attribute, their own value in the address attribute, and their own value in the bank balance attribute.
So, in conclusion I recommend you regard objects as different entities existing in their own right. This will fit in well with the UML design techniques when developing software systems. However, I am unable to indicate how objects manifest at runtime with respect to the heap and stack mechanism because unfortunately I do not know. What I can say is that if you do not use self in the definitions of the method header the method cannot exist separately within each object created.
Hope this helps.
Best wishes
Phil
@@johnphilipjones That's a great explanation, Phil. This can be a tricky topic to talk about, so thanks for expanding it (my brain!)
Wow, that's all I can say.
why we need class concept in python. Instead we can use modules that can act as a class. for example in java we use class variable to get access to its field and methods. but in python we can use module name to access variable and methods inside that module. what the advantage of using class concept in python
A module groups functions and data structures into logical collections. If you used a data structure in a module it will be shared by all code within the module. You create instances of classes (i.e. objects) and each object can have its own copy of data structures. This is not possible with modules. Also, the object oriented development process is different to the procedural approach i.e. they are different paradigms. Consequently there is a different development process across the life-cycle for analysis, design and implementation.
why object is created outside class indention can we create instance inside class indention. forgive me for this silly question since i am from java we used to create instance inside class
I am not 100% sure what you are asking here. You can create an instance of a class from within a class definition. Hope that helps.
tank you
That's how Python works. The Class is the blueprint for the object. So to create the object it must be done outside the class. I believe in Java, the objects are created inside the class, it's NOT like this in Python. .
Objects are created OUTSIDE the class definition, unlike Java
thank you for the info