python--Privatization and Property properties

Source: Internet
Author: User

python--Privatization and Property properties

I. Privatization

    • XX: Public variables
    • _x: Single front underline, privatized property or method, from somemodule import * Prohibit import, class object and subclass can access
    • __XX: Double-front underline, avoid naming conflicts with attributes in subclasses, cannot be accessed directly outside (the name reorganization is not accessible)
    • __XX__: The Magic object or attribute of the user's namespace, both before and after the underscore. For example __init__ , __ don't invent this name yourself.
    • Xx_: Single-post underline to avoid conflicts with Python keywords

The private is accessible through name mangling (the intent is to prevent subclasses from accidentally overriding the base class's methods or properties), such as the _class__object) mechanism.

Summarize
    • Property name in parent class __名字 , subclass does not inherit, child class cannot access
    • If an assignment is made to __名字 a subclass, then a property that is the same name as the parent class is defined in the child class
    • _名variables, functions, and classes are from xxx import * not imported when used

Second, attribute property

1. Adding getter and setter methods to private properties
1 classMoney (object):2     def __init__(self):3Self.__money=04 5     defGetmoney (self):6         returnSelf.__money7 8     defSetmoney (self, value):9         ifisinstance (value, int):TenSelf.__money=value One         Else: A             Print("error: Not an integer number")
2. Using property to upgrade getter and setter methods
1 classMoney (object):2     def __init__(self):3Self.__money=04 5     defGetmoney (self):6         returnSelf.__money7 8     defSetmoney (self, value):9         ifisinstance (value, int):TenSelf.__money=value One         Else: A             Print("error: Not an integer number") -Money = Property (Getmoney, Setmoney)

Operation Result:

 from Import Moneyin [2]: in [2]: a = money () in [3]: in [3]: a.moneyout[3]: 0In [4]: A.money = +[5]: a.moneyout[5]:[6]: A.getmoney () out[6]: 100
3. Replace getter and setter methods with property

@propertyBecome a property function, you can assign value to the property to do the necessary checks, and to ensure that the code is clear and short, there are 2 main functions

    • To convert a method to read-only
    • Re-implement a property set and read method, can do boundary determination
1 classMoney (object):2     def __init__(self):3Self.__money=04 5 @property6     defMoney (self):7         returnSelf.__money8 9 @money. SetterTen     defMoney (self, value): One         ifisinstance (value, int): ASelf.__money=value -         Else: -             Print("error: Not an integer number")

Operation Result:

In [3]: A = Money () in [4]: in [4]: in [4]: a.moneyout[4]: 0In [5]: A.money = [6]: a.moneyout[6]: 100

python--Privatization and Property properties

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.