Object-oriented programming languages are often more convenient and safer to use than process-oriented languages when writing large programs. One of the reasons is: class mechanics.
Classes classify and encapsulate numerous data, making a data object a complete individual, close to real life, and highly abstract. However, Python's encapsulation of classes is not good, because all attributes and methods are public and you can access or write at will. You can modify the attributes of the class outside the class, or even add attributes. This is indeed unsettling.
Let’s summarize the solutions after learning.
#1, use 2 underscore prefixes to hide properties or methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
The impact of double underscores on class attributes:
1. Make the attribute only used inside this class, and cannot be directly read or modified by external or subclasses.
2. The attributes of classes that use _ _ will be renamed during implementation. For example, __age in the class will eventually become _A__age (name reorganization). This advantage is: it is usually used when involving Use it in the inherited parent class. This avoids being overridden by subclass properties.
#2. Create manageable properties.
Sometimes we need to do additional checks on the writing of attributes, reject writing of illegal values, and cause exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
We can find: self.__score is where the attribute value is actually stored, and self.score is a function (but it is used Like a property), it is a method for getting and writing property values.
The socre.setter decorated function will also be called during initialization, because the call to self.score appears under the __init__() function
Since self.__score is only used to reference the value of the attribute , can it be named by another name? Such as saveScore.... Of course it is possible, but it is "exposed". We don't want it to be available externally, so we should
add __ to hide it to prevent accidental modification.
Sometimes, if you are sure that a certain class will not involve inheritance, then you can rewrite the above double underline into a single underline. Although it will not achieve the hidden effect, on the one hand, this will not It will trigger the name mangling mechanism,
to avoid making a fuss. On the other hand, starting with an underscore can remind the user that this attribute should not be used directly. Then, it depends on self-awareness.
An instance object can add attributes externally at will.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
In this way, the properties of the object are limited to the interior of the class.
But __slots__ cannot be inherited. Moreover, the original design intention of __slots__ is not the above usage, but to optimize the memory usage when creating a large number (tens of thousands) of objects.
Summary:
As I write, I find that the above techniques are of little significance. The designer of the class is the programmer himself, and so is the user. Therefore, the
reading and writing of the object attributes should be controlled by himself. The class design itself does not require too much protection code, otherwise it will be bloated and Efficiency is reduced. Protection measures should occur outside the class, so that the data received by the class object is always legal, which will be more lightweight and flexible. This is how I feel.
The above article briefly discussing the read and write permissions of Python object data is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more articles on the read and write permissions of Python object data, please pay attention to the PHP Chinese website!