This example describes the private function invocation method in Python. Share to everyone for your reference, as follows:
Like most languages, Python has a private concept:
① private functions cannot be called from outside their modules
② Private class methods cannot be called from outside their classes
③ private properties cannot be accessed from outside their classes
Unlike most languages, a Python function, method, or property is private or public, depending entirely on its name.
If a Python function, the class method, or the name of the property starts with two underscores (but not the end), it is private; all else is public. Python does not have the concept of class method protection (it can only be used in their own classes and subclasses). Class methods are either private (they can only be used in their own classes) or public (can be used everywhere).
In Python, all specialized methods and built-in properties follow a standard naming convention: There are two underscores for both the start and the end. Do not name your own methods and properties in this way; in the back, it will only confuse you (or anyone else). Private methods are private for some reason, but like many other things in Python, their privatization is essentially a matter of habit rather than coercion.
Note that although we should not access the private method of the class from outside the class, we can actually access it. The name of the private method of the class is actually "translated" into the form preceded by a single underscore and a class name. Look at the code below and you'll see.
>>> class A: def __privatefun (self): print (' This is Private function ') >>> a._a__privatefun ( ) This is private function
Do you see it! We call the private method of the class from the outside of the class! This is a very interesting technique, but remember that we should not use it this way.
More interested in Python related content readers can view this site topic: "Python data structure and algorithm tutorial", "Python Socket Programming Skills Summary", "Python function Tips Summary", "Python string manipulation Skills summary", " Python Introductory and Advanced Classic tutorials and Python file and directory Operations Tips Summary
I hope this article is helpful for Python program design.