1
我試過在文檔中找到這個,但並不完全明白。我開始使用類,當我嘗試獲得結果時,我得到的不是實際的'寬度',我可能需要得到,但不知道。聽起來太複雜了,只是爲了得到結果。有人可以建議嗎?想要一個計算結果而不是<綁定方法... Python 2.8
我:
class Rectangle(object):
def __init__(self, pt_ll, pt_ur):
self.ll = pt_ll
self.lr = Point(pt_ur.x, pt_ll.y)
self.ur = pt_ur
self.ul = Point(pt_ll.x, pt_ur.y)
def width(self):
"""Returns the width-float of the Rectangle."""
w = self.ur.x - self.ll.x
return w
然後我把它叫做:
r = Rectangle(Point(0,0), Point(10,10))
print r.ur.x
print r.ll.x
print r.width
,並得到如下的輸出:
10.0
0.0
<bound method Rectangle.width of <__main__.Rectangle object at 0x000000000B5CBC50>>