python绑定方法错误

python绑定方法错误,python,Python,我对Python2.7和类中的def函数有一个问题,因为绑定方法有问题。这是学校的作业:D 代码如下: 从abc导入ABCMeta,abstractmethod 输入数学 class Shapes(object): __metaclass__= ABCMeta @abstractmethod def __init__(self): pass class TwoDShapes(Shapes): def __init__(self):

我对Python2.7和类中的def函数有一个问题,因为绑定方法有问题。这是学校的作业:D

代码如下: 从abc导入ABCMeta,abstractmethod 输入数学

class Shapes(object):
    __metaclass__= ABCMeta
    @abstractmethod

    def __init__(self):
        pass

class TwoDShapes(Shapes):
    def __init__(self):
        pass

class ThreeDShapes(Shapes):
def __init__(self):
        pass
================= Main.py =====================
from BasicClassShapes import*

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        self.perimeter = length*2 + width*2
    def Area(self):
        self.Area = length*width

====================================

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter
    print "This is the area: ", A.Area
    print "\n"
PrintALL()


=============== Result =======================
This is the name of the shape:  Rectangle
This is the length:  10
This is the width:  20
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  <bound method Rectangle.perimeter of <__main__.Rectangle object at 0x03BEFC90>>

This is the area:  `<bound method Rectangle.Area of <__main__.Rectangle object at 0x03BEFC90>>`
类形状(对象):
__元类_uu=ABCMeta
@抽象方法
定义初始化(自):
通过
第二类形状(形状):
定义初始化(自):
通过
第三类形状(形状):
定义初始化(自):
通过
=========================Main.py=====================
从BasicClassShapes导入*
类矩形(两个图形):
定义初始(自身、形状名称、长度、宽度、边数、顶点数):
self.nameOfShape=“矩形”
self.length=长度
self.width=宽度
self.numberofSides=4
self.numberoftexts=4
超级(矩形,自我)。\uuuu初始化
def周界(自身):
自身周长=长度*2+宽度*2
def区域(自身):
面积=长度*宽度
====================================
def PrintALL():
A=矩形(“矩形”、“10”、“20”、“4”、“4”)
打印“这是形状的名称:”,A.nameOfShape
打印“这是长度:”,A.长度
打印“这是宽度:”,A.宽度
打印“这是边数:”,A.numberofSides
打印“这是顶点数:”,A.numberOfVertices
打印“这是周长:”,A.周长
打印“这是区域:”,A.区域
打印“\n”
PrintALL()
===================结果=======================
这是形状的名称:矩形
这是长度:10
这是宽度:20
这是边数:4
这是vertice的编号:4
这是周界:
这是一个领域:``

您可以在周长函数中使用返回值:

def perimeter(self):
    return self.length*2 + self.width*2
然后调用
A.periment()
,而不是
A.periment

print "This is the perimeter: ", A.perimeter()
同样适用于区域

def Area(self):
    return self.length*self.width


print "This is the area: ", A.Area()
编辑:对不起,我匆忙给出了答案,没有检查。下面是对
Rectangle
类和
PrintALL()
函数的有效替换。我还编辑了上面的内容

最好是将数字类型(而不是字符串)传递给函数,并且可以通过使用浮点而不是整数来避免舍入错误

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2.0 + self.width*2.0
    def Area(self):
        return self.length*self.width

def PrintALL():

    A = Rectangle("Rectangle", 10.0, 20.0, 4.0, 4.0)

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL() 
输出:

This is the name of the shape:  Rectangle
This is the length:  10.0
This is the width:  20.0
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  60.0
This is the area:  200.0

如果形状不变,则它们不需要是函数:

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        super(Rectangle,self).__init__()
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        self.perimeter = length*2 + width*2
        self.Area = length*width
将使用您编写的代码。您不需要函数,因为您可以在初始化时计算
周长
区域

from BasicClassShapes import*

###############################################

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = int(length)
        self.width = int(width)
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2 + self.width*2
    def Area(self):
        return  self.length*self.width

###########################################

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

### Specs of Rectangle ###
    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL()

看看这个代码片段,您需要使用()调用函数,函数为p.addition(),变量为p.a

抱歉,这是我第一次使用这个网站==间距太小,因为我不知道如何处理选项卡内容无需担心,请单击帖子下方的“编辑”,并使用
{}
按钮正确格式化代码。有一个预览会告诉你它是否工作,可能只是缺少一些括号的重复。周长和面积是函数,你想打电话给他们谢谢你的帮助,但我们的老师说我们必须包括函数(体积、面积、表面积、周长、打印全部),这是在纸上键入的要求。我想他们需要函数,因为如果修改了长度或宽度,周长和面积值可以通过再次调用它们的函数来重新计算。@feedMe True。如果希望能够修改形状,则它们应该是函数。NameError:全局名称“length”不是defined@LDs
self.length
等定义周长(self):返回self.length*2+self.width*2定义区域(self):返回self.length*self.width打印“这是周长:”,A.permiture()打印“这是区域:”,A.Area()
TypeError:不能将序列与'str'类型的非int相乘。
Ok我用put int()修复了它。self.length=int(长度)self.width=int(宽度)你能帮我吗?
class New:
    def __init__(self,a,b):
        self.a = a
        self.b = b


    def addition(self):
        return (self.a + self.b)

p = New(3,4)

print(p.a)
print(p.b)

print(p.addition())