Python and private methods for private attributes of the object

01. scenarios and define the way

Scenarios

  • In the actual development, the object  of  some of the properties or methods  may only want to  be used inside the object , while  not wishing to be externally accessible to
  • Private property  is  the object  not want to expose  properties
  • Private method  is  an object  does not wish to disclose the  method

Defined way

  • In  the definition of the properties or methods , the  former property name or method name  to increase  two underscores , is the definition of  private  property or method

  

class Women: 

    DEF  __init__ (Self, name): 
        self.name = name
         # Do not ask a woman's age 
        Self. __age = 18 DEF __secret (Self):
         Print ( " my age is d% " .% Self __age ) 
A = Women ( ' Alice ' )
 Print (A) # # private property, can not directly access the external 
# A .__ Age 
# private methods, can not directly access the external 
# A Secret .__ ()

     





02. The method of pseudo-private attributes and private (science)

Tip: In the daily development, do not use this way , access to the object of private property or private method

Python , There is no  real sense  of  private

  • Giving  properties , methods  naming, is the actual  name  to do some special treatment, so that the outside world can not have access to
  • Treatment : in the  name  preceded  _类名 => _类名__名称
Print (a._Women__age)     # private property can not directly access external 
Print (a._Women__secret ())    # private methods can not directly access external

 03.get functions and set functions

External access to the private property issue resolved

functions to get and set function-defined functions, and packaging concept for anastomosis, and named getXXX setXXX

get function: Gets the value

set Function: Set the value of [assignment by value]

Description: essentially an ordinary member function, get a general function returns a value, set parameter settings

class Women: 

    DEF  __init__ (Self, name): 
        self.name = name
         # Do not ask a woman's age 
        Self. __age = 18 DEF setAge (Self): 
        . Self __age = 18 DEF getAge (Self):
         return . Self __age DEF __secret (Self):
         Print ( " my age D% " . Self% __age ) 
a = Women ( ' Alice ' )
 Print (a.getAge ()) "" "18 is

    

    

     

04. @ property decorator

Decorator: add functionality to dynamic function, a method for members of the class may be used decorator

@property decorator: will function as a variable use

Simplify the get and set functions

Use: @property decorator acting on a function, the function corresponding to get

At the same time it will generate a new decorator @ attribute name .setter, equivalent to the function set for assignment

Note: @property decorative members can only be used in the methods in the class, the simplified code while checking to ensure that the parameters

class the Person:
     DEF  __init__ (Self, name, Age): 
        Self. __name__ = name 
        Self. __age = Age 

    # Note: The naming function, get function, 
    # named function is not fixed, as long as a valid identifier can be, 
    # but, under normal circumstances, the use of variable names named, easy to distinguish 
    @property
     DEF name (Self):
         return . Self __name__ 

    # Note: the naming function: the role and function names @property consistent 
    # decorator naming: @ attribute name .setter 
    # equivalent function is set, set the parameters, assigned to the privatization of property 
    @ name.setter
     DEF name (Self, name): 
        . Self __name__ = name

    @property
    def age(self):
         return self.__age

    @age.setter
    def age(self, age):
        if age < 0:
            age = 0
        self.__age = age
p1 = Person("jack", 15)
print(p1.name)     #>>jack
p1.name = 'tom'
print(p1.name)     #>>tom

 

Guess you like

Origin www.cnblogs.com/huiyichanmian/p/11286339.html