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:
- The parent class has a property name of __, the subclass does not inherit, and the subclass cannot access
- 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
- 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