+ 1
Return self in python?
In class methods what is the significance of return self, what does it do?
1 ответ
+ 2
It returns a new object of class which is written "cls" typically not "self".
That's because you could call class methods and attributes on it's return value:
class Rectangle:
...
@classmethod
def new_square(cls, side_length):
return cls(side_length, side_length)
square = Rectangle.new_square(5)
print(square.calculate_area())