When we define a class, sometimes we define a private attribute to assist development. Private is often used in other languages to modify this attribute as a private attribute. But do you know? The same is a private attribute, some development languages are true private attributes, and some are fake private attributes.
For example: the private attributes appearing in the OC development process are fake private attributes, which can be easily obtained and modified through runtime. At the same time, private attributes in swift are true private attributes, and usually need to be modified by private.
So in python development, how do we define a private attribute? As a convention, we usually add two underscores in front of the attribute, that is, __age represents a private attribute.
You may have questions, is this property a true private property or a fake private property?
First, we define a class and initialize two properties in the class, one of which is private. As follows:
classPerson(object):
# Constructor
def__init__(self,name):
self.name=name
self.__age=18
Create an object and initialize the name attribute.
obj=Person("lily")print(obj.name)
Run the module and it will print out: lily
So how to access private properties:
print(obj.__age)
After running the module, the console will print out:
‘Person’ object has no attribute ‘__age’
Since age is a private attribute, this attribute cannot be accessed directly.
The correct calling method is:
print(obj._Person__age)
The console can print out: 18
Similarly, you can re-assign private attributes
obj._Person__age = 20
print(obj._Person__age)
The console can print out: 20
If we create another class and inherit Person, how can we access our private properties and make changes?
classStudent(Person):def__init__(self):
self.__gender='male'
stu=Student()print(stu._Student__gender)
The console will print out: male
What if you access the private properties of the parent class?
print(stu._Person__age)
At this time, the console will print:'Student' object has no attribute'_Person__age'
This further proves that private properties cannot be inherited.
It can be seen that private attributes are fake private attributes in Python. Then why not guarantee the privacy of the private field syntactically? In the simplest sentence: We are all consenting adults here. As the Python programmer's point of view: it is better to be open than closed.
In summary:
The Python compiler cannot strictly guarantee the privacy of the private field.
Only when the subclass is not under your control, you can consider using the private attribute to avoid name conflicts.
Example extension:
#! encoding=UTF-8classA:
def __init__(self):
# Define private attributes
self.__name ="wangwu"
# Common attribute definition
self.age =19
a =A()
# Normal output
print a.age
# Prompt that the attribute cannot be found
print a.__name
Execution output:
Traceback (most recent call last):
File “C:\Users\lee\Documents\Aptana Studio 3 Workspace\testa\a.py”, line 19, in <module
print a.__name
AttributeError: A instance has no attribute ‘__name‘
Recommended Posts