+ 2

is there a way to initialize the last function? i dont have any idea how to do that.

https://code.sololearn.com/cC8s8zhvu3Cc/?ref=app

28th Dec 2022, 10:38 PM
Nariman Tajari
Nariman Tajari - avatar
8 Respuestas
0
i mean what if i wanted to use static method before the area function or even @classmethod
29th Dec 2022, 1:14 PM
Nariman Tajari
Nariman Tajari - avatar
+ 4
(1) I'm assuming that you want to know if there is a way to call the 'area' method of the 'Shape' class, yes you can but there are some problems with your code. class shape: def __init__(self,weight,height): self.w=weight self.h=height def area(self): return self.height*self.weight Firstly, in the constructor of your 'shape' class, the properties you have defined to take arguments are: self.w AND self.h, HOWEVER, in your 'area' method, you are trying to call self.height and self.weight properties which do not exist in the 'shape' class. Instead, you should be calling self.w AND self.h in 'area' method. In the 'shape' class, there is no property named self.weight OR self.height, the properties are self.w AND self.h. So in order to properly call the 'area' method, you must ensure the property names match in the 'area' method and the constructor of 'shape' class.
28th Dec 2022, 11:44 PM
Natan 🇹🇹
Natan 🇹🇹 - avatar
+ 2
(2) Once you have successfully corrected the property names in your 'area' method. The second problem is that you are trying to call shape.area(w,h) but this is incorrect, the area method takes ONLY 1 argument(self), NOT 2. Instead you must pass the 2 arguments into the shape class AND THEN call the area method. i.e YOUR CODE ---> print(shape.area(w,h)) CORRECTION ---> print(shape(w,h).area()) In the correction code, rather than calling the area method with 2 arguments, we have created an instance of the shape class while giving it the 2 arguments AND THEN call the area method. In the constructor of the shape class, you have already defined 2 properties to take incoming arguments, as such, the area method, will take the 2 arguments FROM shape class and then print the result, hence there is no need to pass 2 arguments into area method, we only need to pass them into shape class. Hope this helps !
29th Dec 2022, 12:00 AM
Natan 🇹🇹
Natan 🇹🇹 - avatar
+ 1
(1) Nariman Tajari Yes you can create the area method as a static method. However, static methods do not have access to any instance specific attributes of the class, they only have access to class-level attributes. So in order to access area as a static method of shape class you can re-use this line from your original code YOUR CODE --> print(shape.area(w,h) And you must also define the attributes you wish to pass into the static method. In order to call area as a static method, your code can look like this: class shape(): @staticmethod def area(width,height): return width * height print(shape.area(24,20)) The above code will output 480. As you can see I have not defined a constructor method for shape class, this is because shape class only has the area static method in it, which cannot access any instance-specific attributes from shape class, hence there is no need to use a constructor. Hope this helps!
29th Dec 2022, 2:04 PM
Natan 🇹🇹
Natan 🇹🇹 - avatar
0
thanks Natan but do you know what if use @staticmethod ?
29th Dec 2022, 12:00 PM
Nariman Tajari
Nariman Tajari - avatar
0
Nariman Tajari .. Not sure what you are asking
29th Dec 2022, 12:16 PM
Natan 🇹🇹
Natan 🇹🇹 - avatar
0
like this i mean: class shape: def __init__(self,weight,height): self.w=weight self.h=height @staticmethod def area(self): return self.w*self.h w=int(input()) h=int(input()) print(shape(w,h).area())
29th Dec 2022, 1:18 PM
Nariman Tajari
Nariman Tajari - avatar
- 1
........... Mmmmmm? MImm
30th Dec 2022, 10:10 AM
Mahathi Reddy