【问题标题】:Getting "'tuple' object has no attribute 'perimetre'?"获取“'元组'对象没有属性'周长'?”
【发布时间】:2022-01-20 04:01:24
【问题描述】:

如何解决问题? 我试试:

class plot():
    def __init__(self,length,width):
    self.length = length
    self.width = width
    
    def area(self):
    print("area is ",self.length*self.width)
    
    def perimetre(self):
    per = 2(self.length+self.width)
    print("perimeter is ",per)
a1 , p1 = plot(10,20),(10,20)

a1.area()
p1.perimetre()

得到了这个错误:

<>:10: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? 
<>:10: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <ipython-input-83-8515c6a74849>:10: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
   per = 2(self.length + self.width)
 --------------------------------------------------------------------------- AttributeError
 Traceback (most recent call last)
  <ipython-input-83-8515c6a74849> in <module>
      13 
      14 a1.area()
 ---> 15 p1.perimetre()
 
 AttributeError: 'tuple' object has no attribute 'perimetre'

【问题讨论】:

  • 正确格式化代码。使用正确的代码格式来包含堆栈跟踪。

标签: python attributes tuples attributeerror


【解决方案1】:

a1, p1 = plot(10,20),(10,20) 更改为a1, p1 = plot(10,20), plot(10,20)。您正在为a1 实例化一个plot,但随后您只使用括号为p1 实例化一个元组。

这里的代码per = 2(self.length+self.width) 也是无效的。您正在尝试调用2,就像它是一个函数一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-27
    • 2022-01-21
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2021-07-30
    相关资源
    最近更新 更多