Advanced day2 python

@property decorator

# Decorator when the need to pay attention to:
  # 1 decorator names, function names needs to be consistent.
  # 2. Property need to declare, write setter, the order can not be reversed
  # 3. If you want a little variable but can not be accessed is modified, you can use the device to access the @Property
  # 4. If you want to modify the accessor variables can build a modifier, or remove access control.

class Person(object):
    def __init__(self, name, age):
        self._name = name
        self._age = age
    # Accessor - getter method for
    the @Property
    DEF name (Self):
        return self._name
    # Accessor - getter method for
    the @Property
    DEF Age (Self):
        return self._age
    # Modifier - setter Method
    @ age.setter
    DEF Age (Self, Age):
        self._age = Age
    Play DEF (Self):
        IF self._age <= 16:
            Print ( '% S is playing chess flight.'% self._name)
        the else:
            Print ( '% S doudizhu being played.'% self._name)
 
 
2. in python class is dynamic,
class Num(object):
    def __init__(self):
        self.a = 1000
   
    def A(self):
        print(self.b)

num = num ()
num.b = 1000000
# print (num.)
Print (num.b)
num. ()
 
 
__solots__ magic
If we need to define custom objects can only bind certain types of properties, defined in the class can be _ _ slots _ _ variables are defined
class Person(object):
    # Person object is defined only bind _name, _age and _gender properties
    __slots__ = ( '_name', ' _age', '_gender')
    def __init__(self, name, age):
        self._name = name
        self._age = age
    @property
    def name(self):
        return self._name
    @property
    def age(self):
        return self._age
    @age.setter
    def age(self, age):
        self._age = age
    Play DEF (Self):
        IF self._age <= 16:
            Print ( '% S is playing chess flight.'% self._name)
        the else:
            Print ( '% S doudizhu being played.'% self._name)
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/lgf333/p/11318916.html