【问题标题】:python bound method Errorpython绑定方法错误
【发布时间】:2017-07-19 21:46:42
【问题描述】:

我对 python 2.7 和类中的 def 函数有疑问,因为绑定方法存在问题。这是学校的作业:D

代码如下:

从 abc 导入 ABCMeta,抽象方法 导入数学

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>>`

【问题讨论】:

  • 对不起这是我第一次使用这个网站= =
  • 间距太小,因为我不知道如何处理选项卡
  • 不用担心,点击帖子下方的“编辑”并使用{} 按钮正确格式化代码。有一个预览会告诉你它是否有效
  • Bound method error的可能重复
  • 只是缺少一些括号。 perimeter 和 Area 是函数,你要调用它们

标签: python


【解决方案1】:

您可以在外围函数中使用返回值:

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

然后调用A.perimeter() 而不是A.perimeter

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

【讨论】:

  • NameError: 未定义全局名称“长度”
  • @LDs self.length
  • def perimeter(self): return self.length*2 + self.width*2 def Area(self): return self.length*self.width print "这是周长:", A .perimeter() print "这是区域:", A.Area() TypeError: can't multiply sequence by non-int of type 'str'
  • 好的,我用 int() 修复了它。 self.length = int(length) self.width = int(width)
  • 你能帮我解决这个问题吗? stackoverflow.com/questions/42519118/…
【解决方案2】:

如果形状不会改变,它们就不需要是函数:

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

将使用您编写的代码。您不需要函数,因为您可以在 perimeterArea 初始化时对其进行数学运算。

【讨论】:

  • 感谢您的帮助,但是我们老师说我们必须包括功能(体积,面积,表面积,周长,打印全部),这是在纸上打出的要求。
  • 我猜他们想要函数,因为如果修改了长度或宽度,则可以通过再次调用它们的函数来重新计算周长和面积值。
  • @feedMe 是的。如果您希望能够修改形状,它们应该是函数。
【解决方案3】:
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()

【讨论】:

    【解决方案4】:
    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())
    

    看这段代码sn-p,需要用()调用函数,函数为p.addition(),变量为p.a

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2016-04-17
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多