Basic features of the python function property

  Function property

  1. In order to protect the property, let it not be random changes (a.width = xxx) (at least, to be subject to certain conditions), so we introduced a set and get method, although this requires a custom (as shown in set_size, get_size method).

  >>> class Rectangle:

  ... def __init__(self):

  ... self.width=0

  ... self.height=0

  ... def set_size(self,size):

  ... self.width,self.height=size

  ... def get_size(self):

  ... return self.width,self.height

  ...

  >>> r=Rectangle()

  >>> r.width=10

  >>> r.height=5

  >>> r.get_size()

  (10, 5)

  >>> r.set_size((150,100))

  #note:

  #r.set_size((150,100))即:

  # Self.width, self.height = (150,100) i.e., 150,100 or:

  #self.width=150,self.height=100

  >>> r.width

  150

  >>> r.height

  100

  2. But this set and get property values ​​(here refers to the value of size) is too cumbersome, if you want to set and get the value of multiple attributes, to use very many times set and get methods, so, here, and we'll set get method of the package together, so that users like the width and height as fast access and assignment.

  >>> class Rectangle:

  ... def __init__(self):

  ... self.width=0

  ... self.height=0

  ... def set_size(self,size):

  ... self.width,self.height=size

  ... def get_size(self):

  ... return self.width,self.height

  # Using the property function, the size of the get and set methods are packaged into this variable size

  ... size=property(get_size,set_size)

  ...

  >>> r=Rectangle()

  >>> r.width=10

  >>> r.height=5

  >>> r.size # quick access size, get the value of size, do not care about the details of the function to get the value of the internal size

  (10, 5)

  >>> r.size=150,100

  >>> r.width

  150

  >>> r.size = (100,50) # to quickly set the value of size, you do not care about the details of the function of the internal settings of size

  >>> r.width

  100

  >>> r.height

  50

  Tips-- parameters of the issue of property:

  class property([get[, set[, del[, doc]]]])

  #Note:

  # 1.get - get the function attribute values

  Function sets the property value - # 2.set

  # 3.del - delete property values ​​function

  # 4.doc - attribute descriptions

  When no arguments are passed, such as: size = property (), the characteristic size of the created neither read nor write.

  Pass only when a parameter such as: size = characteristic size property (get_size), is created will be read-only.

  Passing three parameters, i.e. transmission set, get, and del.

  Passing four parameters, i.e. transmission set, get, del and DOC (document string that describes the properties directly into the string to the contents information). Such as:

  size = property(get, set, del, "I'm the 'x' property.")

  Static methods and class methods Zhengzhou crowd how much money http://mobile.zyyyzz.com/

  >>> class MyClass:

  ... def meth():

  ... print("This is a common method")

  # Create a static method

  Method # a: manually replace

  ... def smeth():

  ... print("This is a static method")

  ... smeth=staticmethod(smeth)

  # Method Two: Use decorator

  ... # @staticmethod

  ... # def smeth():

  ... # print("This is a static method")

  # Create a class method

  Method # a: manually replace

  ... def cmeth(cls):

  ... print("This is a class method")

  ... cmeth=classmethod(cmeth)

  # Method Two: Use decorator

  ... # @classmethod

  ... # def cmeth(cls):

  ... # print("This is a class method")

  ...

  # Class by direct access method

  >>> MyClass.meth()

  This is a common method

  >>> MyClass.smeth()

  This is a static method

  >>> MyClass.cmeth()

  This is a class method

  # Class instances by accessing method

  >>> myclass=MyClass()

  >>> myclass.meth () # Example myclass will itself meth passed as an argument to the method, the method does not define parameters meth self it, resulting in abnormal

  Traceback (most recent call last):

  File "", line 1, in

  TypeError: meth() takes 0 positional arguments but 1 was given

  >>> myclass.smeth()

  This is a static method

  >>> myclass.cmeth()

  This is a class method


Guess you like

Origin blog.51cto.com/14335413/2455945