Whats the point of using a getter function when you can access the variable by just saying self.value. You don’t need to make a function that returns self.value.
Can someone explain this please?
Thanks
Whats the point of using a getter function when you can access the variable by just saying self.value. You don’t need to make a function that returns self.value.
Can someone explain this please?
Thanks
It’s a thorough topic which I’ll link a discussion to below. One of my favorite reasons is:
Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.
It’s fairly common for me to need to ensure that the values are get have valid parameters. That’s something that’s easy to maintain with getter/setter. Especially if the program involves outside user inputs and interactions.
Another great reason for a getter is if you want to run another piece of code whenever the get function is called. If you need to add one to a counter or something and you made a method for that you could put that method in the getter and effectively log how many times that method was done in the program.