[Solved] What is wrong with my class code [Python]?


You need to pass argument to your constructor.

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

It’s not a big problem.

Update

I rewrite your class:

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)

I think you can realize what is your real question after reading my code. And there is a test for the code above.

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

3

solved What is wrong with my class code [Python]?