Python property function usage example analysis

  • 2020-10-31 21:48:24
  • OfStack

This article illustrates the use of property functions in Python. To share for your reference, the details are as follows:

In general, we access and assign attributes both directly and in the class (instance) __dict__ Dealing with, or dealing with data descriptors and so on. But if we were to standardize these access and set methods, one would be by introducing a complex data descriptor mechanism, and the other would be the lightweight data descriptor protocol function Property(). Its standard definition is:

+ property(fget=None,fset=None,fdel=None,doc=None)
The first three arguments are all unbound methods, so they can be virtually any class member function

The first three arguments to the property() function correspond to each of the values in the data descriptor __get__ . __set__ . __del__ Method, so there will be an internal mapping to the data descriptor between them.

So to sum up, actually property() Functions are primarily used to normalize access to class properties and to modify the value of class properties.

property() Function can be used to 0,1,2,3,4 parameter to invoke, order is get, set, del, doc, these four.

property() There are two ways to implement, see the code

1 species:


#!/usr/bin/python
#coding: utf-8
class Rectangle(object):
  def __init__(self, width, height):
    self.width = width
    self.height = height
  def getSize(self):
    return self.width, self.height
  def setSize(self, size):
    self.width, self.height = size
  def delSize(self):
    del self.height
  size = property(getSize, setSize, delSize, " Instance objects ")
r = Rectangle(10, 20)
#  Outputs the length and width of the rectangle at this point 
#  So what we're going to do is getSize
print r.size
#  Modify the size The value of the 
#  So what we're going to do is setSize
r.size = 100, 200
print r.size
del r.height
print r.width
# height Property has been deleted and the following statement will report an error 
# print r.size

Operation results:

[

(10, 20)
(100, 200)
100

]

Type 2 :(decorator)


#!/usr/bin/python
#coding: utf-8
class Rectangle(object):
  def __init__(self, width, height):
    self.width = width
    self.height = height
  #  The following add @ The function name of the symbol should be the same 
  #  The first 1 One is get methods 
  @property
  def Size(self):
    return self.width, self.height
  #  This is a set Method, it is @property A by-product of 
  @Size.setter
  def Size(self, size): #  So we're receiving theta 1 A tuple 
    self.width, self.height = size
  @Size.deleter
  def Size(self):
    del self.width
    del self.height
r = Rectangle(10, 20)
print r.Size
r.Size = 100, 200
print r.Size
del r.height
#  Due to the 1 Step removed self.height Property, so it will report an error when it is accessed again 
# print r.Size
#  You can visit width "Has not been deleted 
print r.width

Operation results:

[

(10, 20)
(100, 200)
100

]

More about Python related content interested readers to view this site project: introduction to Python object-oriented programming and advanced tutorial ", "Python data structure and algorithm tutorial", "Python function using techniques", "Python string skills summary", "Python coding skills summary" and "introduction to Python and advanced tutorial"

I hope this article has been helpful for Python programming.


Related articles: