class animals():
def __init__(self,name,height):
self.name= name
self.height=height
@classmethod
def legs(cls):
print("has four legs")
#animals.legs=classmethod(animals.legs)
animals.legs()
Output
has four legs
or create a class method like this
animals.legs=classmethod(animals.legs)
animals.legs()
Class methods are useful when you need to have methods that aren’t specific to any particular instance, but still involve the class in some way
class animals():
home='Jungle'
def __init__(self,name,height):
self.name= name
self.height=height
@classmethod
def legs(cls):
print("has four legs: stays in ",cls.home)
animals.legs()
Output
has four legs: stays in Jungle
In staticmethod() we are not going to pass any instance of class to it. Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.