+ 1

Lost in classmethod

class Rectangle: def __init__(self, width, height): self.width = width self.height = height def calculate_area(self): return self.width * self.height @classmethod def new_square(cls, side_length): return cls(side_length, side_length) square = Rectangle.new_square(5) print(square.calculate_area()) I cant understand anything...What does @classmethod and the codes below do and why the output is 25? please explain me in easy way....

29th Jul 2016, 2:01 PM
Jason Ahn
3 Réponses
+ 2
The @classmethod in this case is defining a method for handling squares. Since squares are rectangles that have sides of equal length they have defined a class method for handling them. The side length is then assigned to both the width and height and the area is calculated. This is why the output is 25.
7th Aug 2016, 1:12 PM
James Oquinn
0
never easy explain when in c
5th Aug 2016, 1:37 PM
Tammie Younger
Tammie Younger - avatar
0
Thank you so much:)
14th Aug 2016, 12:31 AM
Jason Ahn