Return self in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Return self in python?

In class methods what is the significance of return self, what does it do?

13th Nov 2019, 8:31 AM
prakash ks
prakash ks - avatar
1 Answer
+ 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())
13th Nov 2019, 9:23 AM
Qasem