Python Private Attributes, Private Methods
Private attributes, methods - Python does not really support privatization, but can be underlined to get pseudo-privatization
In practical development, some attributes or methods of a class may only be used internally, rather than accessed externally.
Private attributes are attributes that classes do not want to expose
Private methods are methods that classes do not want to expose
Python can't create private attributes using private modifiers like Java, but Python has a simple mechanism to avoid accidental overwriting of "private" attributes by subclasses.
For example, there is a class called Dog, which uses mood instance attributes internally, but does not open it up. Now, you create a subclass of the Dog class: Beagle. If you create an instance attribute called mood without knowing it, you will override the mood attribute of the Dog class in the inherited method. This is a difficult problem to debug.
To avoid this situation, if you name an instance attribute in the form of _ mood (two leading underscores, no or at most one underscore at the end), Python stores the attribute name in the _ dict attribute of the instance, and adds an underscore and class name before it. Therefore, for the Dog class, _ mood becomes Dog mood; for the Beagle class, it becomes Beagle mood. This language feature is called name mangling.
The member variables that start with a single underscore are called protected variables, which means that only class objects (i.e. class instances) and subclass objects can access these variables by themselves and need to be accessed through the interface provided by the class; they cannot be imported with'from module import *'.
Double underscores start with private members (variables, methods), meaning that only class objects can access the data themselves, and even subclass objects cannot access the data.
class Person: _a = 'hello' #Protect variables, generally not accessed outside the class #A member variable starting with a single underscore is called a protected variable, which means that only class objects (i.e., class instances) and subclass objects can access these variables by themselves, and they need to be accessed through the interface provided by the class; they cannot be imported with'from module import *'. __b = 'world' ##private variable #"Double underscores" begin with private members (variables, methods), meaning that only class objects can access the data themselves, and even subclass objects cannot access the data. name = 'gaozhiyuan' gaozhiyuan = Person() print(Person.__dict__) #For private attributes starting with double underscores, Python stores the name of the attribute in the _dict_ attribute of the instance, and precedes it with an underscore and class name. print(Person._a,gaozhiyuan._a) gaozhiyuan._a = 7 #Modify instance attributes without modifying class attributes print(gaozhiyuan._a,Person._a)#Protected variables can still be modified, only to abide by the conventions and rules outside not to access and modify, otherwise easy to go wrong. print(gaozhiyuan.name) print(gaozhiyuan._Person__b) #The following modifications to instance attributes do not modify class attributes gaozhiyuan._Person__b = 8 #Private methods and attributes can not be called directly by attributes or method names outside. Private methods and attributes are added with "_class name" inside. print(gaozhiyuan._Person__b,Person._Person__b)
{'__module__': '__main__', '_a': 'hello', '_Person__b': 'world', 'name': 'gaozhiyuan', '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None} hello hello 7 hello gaozhiyuan world 8 world
Python public methods can be directly invoked by Python instances; private methods and attributes can not be invoked by attributes or method names externally; private methods and attributes are added with "_class name" internally.
print(dir(Person))
['_Person__b', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_a', 'name']
Private attributes can also be modified, but generally not modified.
#Private attributes can also be modified, but generally not modified. class Person: _a = 'hello' #Protect variables, generally not accessed outside the class #A member variable starting with a single underscore is called a protected variable, which means that only class objects (i.e., class instances) and subclass objects can access these variables by themselves, and they need to be accessed through the interface provided by the class; they cannot be imported with'from module import *'. __b = 'world' ##private variable #"Double underscores" begin with private members (variables, methods), meaning that only class objects can access the data themselves, and even subclass objects cannot access the data. name = 'gaozhiyuan' gaozhiyuan = Person() Person._a = 9 print(Person._a,gaozhiyuan._a) #After the protective variable row of the class is modified, the attributes of the instance are also modified. Person._Person__b = 10 print(Person._Person__b,gaozhiyuan._Person__b)#After modifying the private attributes row of the class, the attributes of the instance are also modified.
9 9 10 10