Private function call method decryption in Python

Source: Internet
Author: User
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.

  • Related Article

    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.