Python3 Privatization Property

Source: Internet
Author: User

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__, do not invent such a name yourself.
    • Xx_: Single-post underline to avoid collisions with Python key-value words.
#!/usr/bin/env Python3classPerson (object):def __init__(self, name, age, taste): Self.name=name Self._age=Age self .__taste=TastedefShowperson (self):Print(Self.name)Print(self._age)Print(self.)__taste)    defDoWork (self): Self._work () self.__away()    def_work (self):Print('my_work')    def __away(self):Print('My__away')classStudent (person):defConstruction (self, name, age, taste): Self.name=name Self._age=Age self .__taste=Tastedefshowstudent (self):Print(Self.name)Print(self._age)Print(self.)__taste) @staticmethoddeftestbug (): _bug.showbug ()#can be accessed within the module, not imported when from cur_module import *class_bug (object): @staticmethoddefshowbug ():Print('Showbug') S1= Student ('Jack', 25,'Football') S1.showperson ()Print('*'*20)#Unable to access __taste, resulting in an error#s1.showstudent ()S1.construction ('Rose', 30,'Basketball') S1.showperson ()Print('*'*20) s1.showstudent ()Print('*'*20) Student.testbug ()

Summarize:

    1. The parent class has a property name of __, the subclass does not inherit, and the subclass cannot access
    2. If you assign a value to an __ name in a subclass, a property that is the same name as the parent class is defined in the child class
    3. The variables, functions, and classes of the _ name are not imported when using the From XXX import *.

Attribute Property

Private properties Add getter and Setter methods

#!/usr/bin/env Python3classMoney (object):def __init__(self): self.__money=0defGetmoney (self):returnSelf.__money    defSetmoney (self, value):ifisinstance (value, int): self.__money=valueElse:            Print('error: Not an integer number')

Using property to upgrade getter and setter methods

#!/usr/bin/env Python3classMoney (object):def __init__(self): self.__money=0defGetmoney (self):returnSelf.__money    defSetmoney (self, value):ifisinstance (value, int): self.__money=valueElse:            Print('error: Not an integer number') Money= Property (Getmoney, Setmoney)

Use property instead of getter and setter methods

@property become an attribute function, you can make the necessary checks when assigning a value to a property, and ensure that the code is clear and short, there are two main functions

    • To convert a method to read-only
    • Re-implement a property set and read method, can do boundary determination

#!/usr/bin/env Python3classMoney (object):def __init__(self): self.__money=0 @propertydefMoney (self):returnSelf.__money@money. SetterdefMoney (self, value):ifisinstance (value, int): self.__money=valueElse:            Print('error: Not an integer number')

Python3 Privatization Property

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.