First, consider the following Rectangle class:
class Rectangle:
def __init__(self):
self.width = 0
self.height = 0
def setSize(self, size):
self.width, self.height = size
def getSize(self):
return self.width, self.height
>>> r = Rectangle()
>>> r.width = 10
>>> r.height = 5
>>> r.getSize()
(10, 5)
>>> r.setSize((150, 100))
>>> r.width
150
This code isn't directly wrong, but it is flawed. The programmer using this class shouldn't need to worry about how it is implemented.(encapsulation). It would be impractical (and kind of silly) if you had a lot of simple attributes, because you would need to write many accessors that did nothing but retrieve or set these attributes, with no useful action taken.
Luckily, Python can hide your accessors for you, making all of your
attributes look alike. Those attributes that are defined through their accessors are often called properties.
__metaclass__ = type
class Rectangle:
def __init__(self):
self.width = 0
self.height = 0
def setSize(self, size):
self.width, self.height = size
def getSize(self):
return self.width, self.height
size = property(getSize, setSize)
After this, you no longer need to worry about how things are implemented, but can treat width, height, and size the same way:
>>> r = Rectangle()
>>> r.width = 10
>>> r.height = 5
>>> r.size
(10, 5)
>>> r.size = 150, 100
>>> r.width
150
As you can see, the size attribute is still subject to the calculations in getSize and setSize,but it looks just like a normal attribute. T
hat is to say, property function make "attribute" the interface of class. The moral is this: with new-style classes, you should use property rather than accessors. Considering more about property,it isn't really a function—it's a class whose instances have some magic methods that do all the work.
The methods in question are __get__, __set__, and __delete__. Together, these three methods define the so-called descriptor protocol.
In fact, the mechanism underlying properties, bound methods, static and class methods, and super.