I've started learning Python recently, and there are some topics I cannot really understand. One is the usage of the decorators in user defined objects, or encapsulation more generally. I mean, let's consider for example:
class Dog:
def __init__(self,name,age):
self.name=name
self.age=age
Ok so now I have the object 'Dog'. If I make its attribute privates by adding '__' in the constructor, I won't be able to access them. In order to do so, I will have to use, for example, the decorator 'property', which has attributes (not sure how to call them actually)'getter', 'setter' and 'deleter'. So I make, for example, the name private. I have:
class Dog:
def __init__(self,name,age):
self.__name=name
self.age=age
@property
def name(self):
return self.__name
@name.setter
def name(self,name)
self.__name=name
@name.deleter
def name(self)
del self.__name
Now the question is: how is this different from just letting name be public? I would understand if I could only read the name value, or don't delete it, but if I give the user the possibility to read, write and delete the attribute... why do I bother making it private in the first place?
I don't know, maybe this was just a "syntax exercise" and in fact you are meant to give the user only a proper subset of those abilities.
__name_mangling
unless that's really the specific behaviour you want: python.org/dev/peps/pep-0008/#designing-for-inheritance).