The privatization of Python __python

Source: Internet
Author: User

1, double underline (__)
Properties that start with a double underline are confused at run time, so direct access is not allowed. Properties that actually start with a double underline
Will precede the explanation with an underscore and a class name, such as Self.__num will be parsed as Self._classname__num,
2, Single underline (_)
Simple module-level privatization you only need to use a single underline character before the property name. This prevents the properties of the module from being
From mymodule Import * loaded. Because this is strictly based on the scope, it is also useful for functions.
3. Authorization
Authorization is a feature of packaging that can be used to simplify processing of dictating functions and to use existing functionality to achieve maximum
Limitation of code reuse.
The key point to implementing authorization is to overwrite the __getattr__ () method, example:

Class Wrapme (object):
    def __init__ (self,obj):
        self.__data=obj
    def __getattr__ (self,attr):
        return GetAttr (self.__data,attr)

w = WRAPME (3.5+4.5j) w.real 3.5 w.imag 4.5 w.conjugate
()
(3.5-4.5j)

The Real,imag,conjugate property passed here is authorized by GetAttr to the Wrapme object. Remove __getattr__
When you access properties such as real, you will be prompted that the property does not exist.

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.