Summary of Python calling private attributes

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

Summary of Python calling private attributes
Summary of logarithm method in Python
A summary of 200 Python standard libraries!
Comprehensive summary of Python built-in exception types
How to define private attributes in Python
Summary of common operations of Python time module
Summary of knowledge points about Python unpacking
7 features of Python3.9
Python basic summary
Method analysis of Python calling C language program
A brief summary of the difference between Python2 and Python3
Python processing json summary
Python interview questions summary
Basics of Python syntax
Python advanced usage summary
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Summary of ubuntu usage
Consolidate the foundation of Python (4)
Python high-order function usage summary!
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Python datetime processing time summary
python king of glory wallpaper
Consolidate the foundation of Python(5)
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Python version of OpenCV installation
Summary of various ubuntu problems
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules
Python high-order function usage summary!