python in property and setter decorator

property and setter decorator

  Role: calling method instead calls the object, such as: p.set_name () instead p.set_name

       Difference: the former changed the get method, the latter method to change the set

  Renderings:
  

  Code:

class Person:
    def __init__(self,name):
        self._name = name

    def get_name(self):
        return self._name

    def set_name(self,name):
        self._name = name

p = Person('小黑')
print(p.get_name())
p.set_name('小灰')
print(p.get_name())
class the Person:
     DEF  the __init__ (Self, name): 
        self._name = name 

    # using the acquired property name decorator method of converting the properties of the object to acquire 
    @Property
     DEF get_name (Self):
         return self._name 

    # using the property set decorator the method name is converted to object attribute obtaining 
    @ get_name.setter
     DEF set_name (Self, name): 
        self._name = name 


p = the Person ( ' black ' )
 Print (p.get_name)    # original p.get_name (), now p .get_name 
p.set_name = ' small gray '  #Original p.set_name ( 'small gray'), now p.set_name = 'Grays' 
Print (p.get_name)

Standard wording:

  Renderings:

  Code:

# Property decorator 
# action: get method to convert a property of the object. It is to call the method was changed to call the object 
# Conditions: must and attribute names as 

# decorator setter methods: 
# Role: to convert a set method for the property of the object. A method is invoked instead call the object 
# Usage: @ attribute name .setter 

class the Person:
     DEF  __init__ (Self, name): 
        self._name = name 

    # using the property decorator will get converted to obtain the method name of the object property 
    @property
     DEF name (Self):
         return self._name 

    # using property name set decorator method of converting the properties of an object to acquire 
    @ name.setter
     DEF name (Self, name): 
        self._name = name


P = the Person ( ' black ' )
 Print (p.name)    # original acquisition p.name (), now p.name 
p.name = ' small gray '  # original set p.name ( 'small gray'), now p.name = 'Grays' 
Print (p.name)

 

Guess you like

Origin www.cnblogs.com/FlyingLiao/p/11334097.html