Private function call method decryption in python, python private
This example describes how to call a private function in python. We will share this with you for your reference. The details are as follows:
Like most languages, Python also has a private concept:
① Private functions cannot be called outside their modules
② Private class methods cannot be called outside their classes
③ Private attributes cannot be accessed from outside their classes
Unlike most languages, a Python function, method, or attribute is private or public, depending on its name.
If a Python function, class method, or attribute name starts with two underscores (but not ends), it is private; all others are 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 (only available in their own classes) or public (available anywhere ).
In Python, all the specialized methods and built-in attributes follow a standard naming convention: the start and end have two underscores. Do not name your own methods and attributes in this way. Later, it will only confuse you (or others. Private methods are private for some reason, but like many other things in Python, their privatization is basically a habit, not forced.
Note that although we should not use the private method of the external callback class of the class, we can actually access it. The names of private methods of classes are actually translated into the form of a single underline and class name. You can see the following code.
>>> Class A: def _ privatefun (self): print ('this is private function') >>> a. _ A _ privatefun () this is private function
No! We called the private method of the class from outside the class! This is an interesting technique, but remember we should not use it like this.