Properties and methods of Python private classes

To make methods and properties private, all you need to do in Python is start with two underscores .

class Demo:
    def __inaccessible(self):
        print("HAHA, YOU CANNOT SEE ME")
    def accessible(self):
        print(The secret message is:)
        self.__inaccessible()

But if you understand the processing mechanism behind, you can still access this private method in the following ways:

s = Demo()
s._Demo_inaccessible()

Therefore, it is generally agreed to use another way to start with an underscore .

Published 89 original articles · won 83 · views 3486

Guess you like

Origin blog.csdn.net/devin_xin/article/details/105505272