This video looks at whether 'setter' and 'getter' methods are needed in Python. It also acts as a review and summary of the last three videos in the Python playlist.
Hundreds of Python tutorials... You are the Superior teacher. Hands down the best explanations for creating solid associations while learning. Thank you for your efforts.
Thank you for your generous comments they are appreciated. Please consider sharing the channel as it helps keep the video series free to view. Best wishes Phil
I've been doing Python for about 5 months now, I bought a Udemy Course, found websites, searched youtube. Couldn't find anything that could explain classes to me, and that was the only thing that was really allowing me to be fluent in the basics of Python. I found these videos, and I'm confident that I'm going to have to basis to do anything in python programming, after I complete all of these videos. Amazing work!
This is the only one of your videos I don’t understand, classes , getters and setters confuse me also functions lol I’m quite new forgive me great video as always tho your pace and detail are legendary!
Think of an object as an entity that is self-contained. Within the object there is behaviour and attributes where the behaviour and attributes are defined in the class. Think of a class as a factory that produces the objects. If you want the object to perform a task (e.g. calculate the charges on an overdraft) you send it a message to invoke one of it behaviours. These behaviours are small segments of code (i.e. methods which is like a function) contained within the object. An attribute again contained within the object is essentially a variable (e.g. it stores the current debit interest rate). At some point you will need to set the current debit rate to a value. When you wish to know what the current debit interest rate is you need to obtain it. Whatever the debit interest rate is it will be stored within the object. One way to set the debit interest rate to a value is to invoke the setter method (which allows you access to the debit interest rate). To find out what the current debit interest rate is you invoke the getter method which again allows you access). Getter and setter methods are typical for most object orientated languages. It is also typical terminology used in UML design diagrams. For this reason I have shown how you can use getter and setter methods in Python. However using such methods is not regarded as pythonic. But many programmers coming from another language to Python often use this approach. A strong feature of object oriented programming is to hide data within attributes of an object. Only allowing access to such data via special mechanisms. This is often achieved in other languages using the reserved word 'private'. Python has not adopted this approach it is possible to obtain direct access to an attribute within a Python object without a special mechanism. However, you can as a programmer decide to use getter and setter approach. This would be a decision for you as an individual programmer and for a programming house. Whatever the programming house decides upon you would be forced to follow suit. Pythonic programmers would not use getters and setters. Best wishes Phil
This is probably not important as I understand how it works as you describe but had to ask this question. When we create the instance and pass the : customer = Account (20, "Rita Jones") It actually is going to : def __init__(self, balance, name): and you would think it was going to: class Account(balance,name): If you were new to this, as am I, you would not see how the name and age were getting to the __init__() as there is no path to follow in the code. Yet another great video thank you each video is like a holiday.
This is a good question. The diagrams I use to explain classes and objects are very much a model that I have effectively created to hide the underlying mechanisms of the Python language. They are based on the messaging model of UML. Therefore they should not be taken too literally the model is a good approximation. Python uses namespaces and dictionaries to ‘control’ the runtime. It is my view that UML does not support the Python ‘model’ well hence my approximation model. But to answer your question from the perspective of the model I would regard the __init__ as being defined by the class with each object being given their own copy with the self parameter holding the id of each instance. In other words the __init__ method knows which object it belongs to because of the id it is passed. Best wishes Phil
I have watched 4 tutorials about OOP, i must admit this one is the best. Mr. can you make something like this but with dictionary or list? Really good work sir.Thank you very much.
New to Python and working on this topic currently to complete programming course, your explanation was great, thanks a lot hopefully you can create hundreds of tutorials, by the way, anything related to MySQL and JavaScript? Kind regards
always best of the best... and I checked your personal website, and looking for some exercises for Class and Object designing, but I couldn't find any. I wonder if you are going to make some in the future? It's ok for me to pay the tuition to your courses :) keep on your splendid teaching work, man !
Once I get to around 200 Python videos (104 at the moment) I will be writing some more exercises. This will be in around 12 to 24 months time. All of the videos are free to view so no tuition fees needed :-). Although you could help sponsor the work on Patreon or via website - all monies go towards maintaining the website, updating hardware and software needed to make the videos etc.Links are below: www.patreon.com/user?u=3538393 www.johnphilipjones.com/donate.html Best wishes Phil
Hello John, i understand that we should strive to use properties in python. Is this assumption correct? Instead of using setters and getters should we use @property and @x.setter decorators ?
Hello Kevin, You are correct decorators are the best approach with Python. The setters and getters are used with many other languages. It is ok to use getters and setters with Python if you (and your programming team) can keep their discipline when writing code. However, Python does not hide data in the way other languages do (for example other languages use the keyword Private). Therefore decorators are a better approach. Best wishes Phil
I have few questions here John Under init we have 2 formal parameters balance which to me keeps changing as and when balance is debited or credited. And name part is constant most of the time unless needed. When we created the instance of this class as below customer = Account (0,"Rita Jones") Self ID must have been created Now when we credit the account using method customer.credit the balance value changes. So does in this case the new object is created with replacing Old ID even though we used method. If that's the case then with each time changes in balance new object is created. Plz help me understand this.
The id of customer will remain the same. Write a segment of code to print the id at various stages of the execution of the program and observe the id's Best wishes Phil
The animations are done in PowerPoint with additional animations overlayed in Camtasia. I am committed to making videos on Python for the next year but I will put the making of videos on how I make the videos in the pipeline. Also, I will need a more Powerful PC to do this which I will purchase next year. The following link has information on how I make the videos. www.pythonbytesize.com/educators.html Best wishes Phil (John Philip Jones)
I used PowerPoint and screen-recording software quite a bit in my prior life, so I appreciate all of the outstanding animation capabilities that PP has, and the ability to record your actions on PP. You've used some of the PP capabilities to greatly enhance your videos. I'm actually most impressed with the planning, design, and implementation of your videos, which can be really time consuming. As an Apple exec once said: Simple is hard. Besides your knowledge of Python, I think it's your planning and story-boarding each video, and division of the subject matter among the videos in the playlist, that are really impressive.
Declaring variables in __init__() can indeed allow other methods declared in the class to be accessed from instances of the class. Of course depending on the design you may not want all methods to access all variables. It depends(in my view) on how the system has been designed using UML diagrams as to the scope of the variables available throughout the class and its instances. So it is not an easy question to answer. Also, I have covered the getter and setter approach here because it tends to fit with UML designs. Of course in Python many frown upon this approach. But that is a question and answer for another time. Best wishes Phil
You are the best Python tutor on CZcams!
Hundreds of Python tutorials... You are the Superior teacher. Hands down the best explanations for creating solid associations while learning. Thank you for your efforts.
Thank you for your generous comments they are appreciated. Please consider sharing the channel as it helps keep the video series free to view.
Best wishes
Phil
I've been doing Python for about 5 months now, I bought a Udemy Course, found websites, searched youtube. Couldn't find anything that could explain classes to me, and that was the only thing that was really allowing me to be fluent in the basics of Python. I found these videos, and I'm confident that I'm going to have to basis to do anything in python programming, after I complete all of these videos. Amazing work!
Thank you for your positive comments I'm glad the videos are helping Best wishes Phil
For beginners this style of teaching is incredibly useful and innovative.
Thank you for your positive comments.
Best wishes Phil
Extremely clear Mr Jones, Thank you. By far the best tutorial that I have visited so far on CZcams (I abandoned the others :)
It finally makes sense! I could cry! 😭
Professional and clear. I am going to use this in my classroom.
It makes sense in the world of Jones, you make programming fun, thanks.
Well executed
Must have put in a lot of work
wonderful... wonderful.... wonderful👏
Thank you for your generous comment.
Best wishes
Phil
Cool stuff! You are awesome. I understood every bit.
This is the only one of your videos I don’t understand, classes , getters and setters confuse me also functions lol I’m quite new forgive me great video as always tho your pace and detail are legendary!
Think of an object as an entity that is self-contained. Within the object there is behaviour and attributes where the behaviour and attributes are defined in the class.
Think of a class as a factory that produces the objects. If you want the object to perform a task (e.g. calculate the charges on an overdraft) you send it a message to invoke one of it behaviours. These behaviours are small segments of code (i.e. methods which is like a function) contained within the object.
An attribute again contained within the object is essentially a variable (e.g. it stores the current debit interest rate).
At some point you will need to set the current debit rate to a value. When you wish to know what the current debit interest rate is you need to obtain it.
Whatever the debit interest rate is it will be stored within the object.
One way to set the debit interest rate to a value is to invoke the setter method (which allows you access to the debit interest rate).
To find out what the current debit interest rate is you invoke the getter method which again allows you access).
Getter and setter methods are typical for most object orientated languages. It is also typical terminology used in UML design diagrams. For this reason I have shown how you can use getter and setter methods in Python.
However using such methods is not regarded as pythonic. But many programmers coming from another language to Python often use this approach.
A strong feature of object oriented programming is to hide data within attributes of an object. Only allowing access to such data via special mechanisms. This is often achieved in other languages using the reserved word 'private'. Python has not adopted this approach it is possible to obtain direct access to an attribute within a Python object without a special mechanism. However, you can as a programmer decide to use getter and setter approach. This would be a decision for you as an individual programmer and for a programming house. Whatever the programming house decides upon you would be forced to follow suit. Pythonic programmers would not use getters and setters.
Best wishes Phil
John Philip Jones this was such a good explanation I feel like if I read it enough times I’ll get it completely
Amazing python Tutor, soo clear!! Thanks!
This is probably not important as I understand how it works as you describe but had to ask this question.
When we create the instance and pass the :
customer = Account (20, "Rita Jones")
It actually is going to :
def __init__(self, balance, name):
and you would think it was going to:
class Account(balance,name):
If you were new to this, as am I, you would not see how the name and age were getting to the __init__() as there is no path to follow in the code.
Yet another great video thank you each video is like a holiday.
This is a good question. The diagrams I use to explain classes and objects are very much a model that I have effectively created to hide the underlying mechanisms of the Python language. They are based on the messaging model of UML. Therefore they should not be taken too literally the model is a good approximation. Python uses namespaces and dictionaries to ‘control’ the runtime. It is my view that UML does not support the Python ‘model’ well hence my approximation model. But to answer your question from the perspective of the model I would regard the __init__ as being defined by the class with each object being given their own copy with the self parameter holding the id of each instance. In other words the __init__ method knows which object it belongs to because of the id it is passed.
Best wishes
Phil
I have watched 4 tutorials about OOP, i must admit this one is the best. Mr. can you make something like this but with dictionary or list? Really good work sir.Thank you very much.
I will put them in the pipeline.
Best wishes
Phil
Excellent tutor. Thanks
nice animation!
creative as always... thanks
OMG thank you!!!!!!!!
New to Python and working on this topic currently to complete programming course, your explanation was great, thanks a lot
hopefully you can create hundreds of tutorials, by the way, anything related to MySQL and JavaScript? Kind regards
Thank you for the positive comments, Video on MySQL are in the pipeline.
Best wishes
Phil
always best of the best...
and I checked your personal website, and looking for some exercises for Class and Object designing, but I couldn't find any. I wonder if you are going to make some in the future? It's ok for me to pay the tuition to your courses :)
keep on your splendid teaching work, man !
Once I get to around 200 Python videos (104 at the moment) I will be writing some more exercises. This will be in around 12 to 24 months time. All of the videos are free to view so no tuition fees needed :-).
Although you could help sponsor the work on Patreon or via website - all monies go towards maintaining the website, updating hardware and software needed to make the videos etc.Links are below:
www.patreon.com/user?u=3538393
www.johnphilipjones.com/donate.html
Best wishes
Phil
Hello John, i understand that we should strive to use properties in python. Is this assumption correct?
Instead of using setters and getters should we use @property and @x.setter decorators ?
Hello Kevin, You are correct decorators are the best approach with Python. The setters and getters are used with many other languages. It is ok to use getters and setters with Python if you (and your programming team) can keep their discipline when writing code. However, Python does not hide data in the way other languages do (for example other languages use the keyword Private). Therefore decorators are a better approach.
Best wishes Phil
keep em coming! Choo choo
great
I have few questions here John
Under init we have 2 formal parameters balance which to me keeps changing as and when balance is debited or credited. And name part is constant most of the time unless needed.
When we created the instance of this class as below
customer = Account (0,"Rita Jones")
Self ID must have been created
Now when we credit the account using method customer.credit the balance value changes. So does in this case the new object is created with replacing Old ID even though we used method. If that's the case then with each time changes in balance new object is created. Plz help me understand this.
The id of customer will remain the same. Write a segment of code to print the id at various stages of the execution of the program and observe the id's
Best wishes
Phil
@@johnphilipjones Thanks John❤. I already checked it. ID remains same.
Thanks. Can you do a video on how you create these wonderful graphics/video that are in your tutorials?
The animations are done in PowerPoint with additional animations overlayed in Camtasia. I am committed to making videos on Python for the next year but I will put the making of videos on how I make the videos in the pipeline. Also, I will need a more Powerful PC to do this which I will purchase next year. The following link has information on how I make the videos.
www.pythonbytesize.com/educators.html
Best wishes
Phil
(John Philip Jones)
I used PowerPoint and screen-recording software quite a bit in my prior life, so I appreciate all of the outstanding animation capabilities that PP has, and the ability to record your actions on PP. You've used some of the PP capabilities to greatly enhance your videos. I'm actually most impressed with the planning, design, and implementation of your videos, which can be really time consuming. As an Apple exec once said: Simple is hard. Besides your knowledge of Python, I think it's your planning and story-boarding each video, and division of the subject matter among the videos in the playlist, that are really impressive.
Thank you for your positive comments
Best wishes
Phil
Isn't it smarter to include all variables under __init__ since we can refer to it from inside any method at any later time?
Declaring variables in __init__() can indeed allow other methods declared in the class to be accessed from instances of the class. Of course depending on the design you may not want all methods to access all variables. It depends(in my view) on how the system has been designed using UML diagrams as to the scope of the variables available throughout the class and its instances. So it is not an easy question to answer. Also, I have covered the getter and setter approach here because it tends to fit with UML designs. Of course in Python many frown upon this approach. But that is a question and answer for another time.
Best wishes
Phil
I can't get this code to run. The error message says
TypeError: Account() takes no arguments
Can you send me the code.
Best wishes
Phil