书中讲property的部分,举例代码
__metaclass__ = type
class Rectangle:
....def __init__(self):
........self.width = 0
........self.height = 0
....def getSize(self, size):
........self.width, self.height = size
....def setSize(self, size):
........return self.width, self.width
....size = property(getSize, setSize)
===
#运行例子:
>>>r = Rectangle()
>>>r.width = 10
>>>r.height = 5
>>>r.size
(10, 5)
>>>r.size = 150, 100
>>>r.width
150
=========================================
1.这里的width和height叫什么东西(怎么称呼),我在运行个r.length时报错说:Rectangle instance has no attribute 'length',attribute和property有区别吗,区别是什么?
2.真心看不出这个size属性有什么必要性,你说一个Rectangle有意义的表征width和height都知道,要size属性有何用,不是很多余吗
对这个property很疑惑,我模糊觉得一个类似width和height的东东就可以替代,但既然有这东东,肯定有什么用途。
__metaclass__ = type
class Rectangle:
....def __init__(self):
........self.width = 0
........self.height = 0
....def getSize(self, size):
........self.width, self.height = size
....def setSize(self, size):
........return self.width, self.width
....size = property(getSize, setSize)
===
#运行例子:
>>>r = Rectangle()
>>>r.width = 10
>>>r.height = 5
>>>r.size
(10, 5)
>>>r.size = 150, 100
>>>r.width
150
=========================================
1.这里的width和height叫什么东西(怎么称呼),我在运行个r.length时报错说:Rectangle instance has no attribute 'length',attribute和property有区别吗,区别是什么?
2.真心看不出这个size属性有什么必要性,你说一个Rectangle有意义的表征width和height都知道,要size属性有何用,不是很多余吗
对这个property很疑惑,我模糊觉得一个类似width和height的东东就可以替代,但既然有这东东,肯定有什么用途。