python 2.7在另一个类中实例化类

python 2.7在另一个类中实例化类,python,Python,我正在编写一个类Tbeam(IPython笔记本2.2.0中的Python 2.7.8),用于计算钢筋混凝土T梁的值。T梁的翼缘和腹板被视为矩形类的对象。我从类Tbeam中的类矩形实例化了一个法兰和腹板,并创建了计算Tbeam的总深度(d)和面积(area)的方法 class Rectangle: """A class to create simple rectangle with dimensions width (b) and height (d). """ def __init__(s

我正在编写一个类Tbeam(IPython笔记本2.2.0中的Python 2.7.8),用于计算钢筋混凝土T梁的值。T梁的翼缘和腹板被视为矩形类的对象。我从类Tbeam中的类矩形实例化了一个法兰和腹板,并创建了计算Tbeam的总深度(d)和面积(area)的方法

class Rectangle:
"""A class to create simple rectangle with dimensions width (b) and 
height (d). """

def __init__(self, b, d ):
    """Return a rectangle object whose name is *name* and default
    dimensions are width = 1, height = 1.
    """
    self.width = b
    self.height = d

def area(self):
    """Computes the area of a rectangle"""
    return self.width * self.height

def inertia(self):
    """Computes the moment of inertia of a rectangle,
    with respect to the centroid."""

    return self.width*math.pow(self.height,3)/12.0

def perim(self):
    """Calculates the perimeter of a rectangle"""
    return 2*(self.width+self.height)

def centroid(self):
    """Calculates the centroid of a rectangle"""
    return self.height/2.0

def d(self):
    """Returns the height of the rectangle."""
    return self.height

def bf(self):
    """Returns the width of the rectangle."""
    return self.width
-

-

当我执行测试单元时

# Test Tbeam
t1 = Tbeam(60.0, 5.0,27.0,12.0)
print t1.d
print t1.area
-

我得到以下信息:

27.0

bound method Tbeam.area of <__main__.Tbeam instance at 0x7f8888478758
27.0

绑定方法Tbeam.area of您正在打印的
t1.area
是一种方法。您希望打印调用该方法的结果,因此
print t1.area()

区域方法定义为

def area(self):
    area =self.flange.area + self.web.area
def area(self):
    area =self.flange.area() + self.web.area()
但应定义为

def area(self):
    area =self.flange.area + self.web.area
def area(self):
    area =self.flange.area() + self.web.area()