Classmethod vs Staticmethod in Python’s OOP

A little code written to understand classmethod and staticmethod

class Area: #defining an area class
def init(self,wt,ht):
self.ht= ht
self.wt= wt
def area(self): #defining a simple rect area method
return sel…


This content originally appeared on DEV Community and was authored by Charles Ukadike

A little code written to understand classmethod and staticmethod

class Area: #defining an area class
def init(self,wt,ht):
self.ht= ht
self.wt= wt
def area(self): #defining a simple rect area method
return self.wt*self.ht

@classmethod
def sqr(cls,sd): #defining a square method to take advantage of the rect area method
    return cls(sd,sd)

class Tri(Area): #defining a triangle class to inherit from the area class
def area(self):
super().area() #calling the rect area method from the super_class "Area"
return int(0.5*self.wt*self.ht) #using the super_class's argument to output the triangle's area
@staticmethod
def check(var): #defining a regular function
if var>=20:
print("This triangle has a large area")
else:
print("Small area detected")

rect= Area(4,5)
sqr= Area.sqr(7) #initiating the square's area
tri= Tri(30,100)

Tri.check(tri.area()) #calling the regular function (initiating the staticmethod). This takes the format: class.method()

tri.check(tri.area()) the above can also be written this way. This takes the format: object.method()

print(rect.area(),sqr.area(),tri.area())

Note 01: To initiate a classmethod or call a staticmethod; the format is as follows "class.method()"

Note 02: To initiate / call other methods or staticmethod, the format is as follows; "object.method()"

Note 03: Note that staticmethods can be called in 2 ways;

By class e.g class.method()

By object e.g object.method()


This content originally appeared on DEV Community and was authored by Charles Ukadike


Print Share Comment Cite Upload Translate Updates
APA

Charles Ukadike | Sciencx (2021-08-26T19:19:09+00:00) Classmethod vs Staticmethod in Python’s OOP. Retrieved from https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/

MLA
" » Classmethod vs Staticmethod in Python’s OOP." Charles Ukadike | Sciencx - Thursday August 26, 2021, https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/
HARVARD
Charles Ukadike | Sciencx Thursday August 26, 2021 » Classmethod vs Staticmethod in Python’s OOP., viewed ,<https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/>
VANCOUVER
Charles Ukadike | Sciencx - » Classmethod vs Staticmethod in Python’s OOP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/
CHICAGO
" » Classmethod vs Staticmethod in Python’s OOP." Charles Ukadike | Sciencx - Accessed . https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/
IEEE
" » Classmethod vs Staticmethod in Python’s OOP." Charles Ukadike | Sciencx [Online]. Available: https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/. [Accessed: ]
rf:citation
» Classmethod vs Staticmethod in Python’s OOP | Charles Ukadike | Sciencx | https://www.scien.cx/2021/08/26/classmethod-vs-staticmethod-in-pythons-oop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.