【问题标题】:What is wrong with my class code [Python]?我的类代码 [Python] 有什么问题?
【发布时间】:2017-10-02 03:27:33
【问题描述】:

这是我在这里的第一篇文章,如果有任何错误,请告诉我。

这是我的代码:

import math
class Square():
    def __init__(self, length):
        self.length = length


    def getL(self,length):
        self.length = length
        print(length)


    def getP(self,length):
        return length * 4

    def getA(self,length):
        return length * length

    def __add__(self,another_square):
        return Square(self.length + another_square.length)

s1 = Square()
s2 = Square()
print(s1.setL(5))
print(s1.getP(5))
print(s1.getA(5))
print(s2.getP(12))
print(s2.getA(15))
s3 = s1 + s2
print(s3.getA())

当我运行时,它给了我一个 TypeError:

Traceback (most recent call last):
File "C:\Users\user\Desktop\programming.py", line 21, in <module>
s1 = Square()
TypeError: __init__() missing 1 required positional argument:'length'

我想知道我的代码有什么问题,我们将不胜感激。

【问题讨论】:

标签: python class typeerror


【解决方案1】:

您需要将参数传递给您的构造函数。

s1 = Square(1) # will call Square.__init__(self, 1)
s2 = Square(2) # will call Square.__init__(self, 2)

这不是什么大问题。

更新

我重写你的课:

import math
class Square():
    def __init__(self, length):    #return an instance of this class with 'length' as the default length.
        self.length = length

    def setL(self,length):    #pass length to the instance and override the original length.
        self.length = length

    def getL(self):           #get the length of instance.
        return self.length

    def getP(self):           #get the perimeter of instance.
        return self.length * 4

    def getA(self):           #get the area of instance.
        return self.length ** 2

    def __add__(self,another_square):     #return a new instance of this class with 'self.length + another_square.length' as the default length.
        return Square(self.length + another_square.length)

我认为您在阅读我的代码后会意识到您真正的问题是什么。并且对上面的代码进行了测试。

s1 = Square(1)
s2 = Square(2)
print(s1.getP())
print(s1.getA())
print(s2.getP())
print(s2.getA())
s3 = s1 + s2
print(s3.getA())

output:
4
1
8
4
9

【讨论】:

  • 谢谢,但是当我现在运行代码时,它给出了一个错误,说 s3 需要另一个位置参数“长度”,但它不应该添加 s1 和 s2 的值并打印它吗?
  • @Assassin'sOrder 什么错误?请更新您的问题。
  • @Assassin'sOrder 我会更新答案,向你解释发生了什么。
【解决方案2】:

您的类 init 需要一个长度参数,而您正在尝试创建 s1 而不给它一个

【讨论】:

    【解决方案3】:

    您应该使用构造函数来传递值。 像这样。

    s1 = Square(100) 
    s2 = Square(482) 
    

    【讨论】:

      【解决方案4】:

      您必须提供长度,因为在 init() 中需要长度参数:

      s1 = Square(5)
      s2 = Square(5)
      

      【讨论】:

        【解决方案5】:

        您正在初始化 Square 的实例,但未指定 length 参数。

        s1 = Square()
        s2 = Square()
        

        根据你的定义

        def __init__(self, length):
            self.length = length
        

        length 是必需的位置参数。您需要为此参数定义一个合理的默认值,例如

        def __init__(self, length=1):
            self.length = length
        

        或在初始化对象时显式提供(例如s1 = Square(1)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多