Private function call method decryption in python, python private

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.