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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.