we can use the class and its properties inside that method rather than a particular instance
classmethod has a reference to a class object as the first parameter. ( staticmethod has no reference )
Example:
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()
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. staticmethod()
Using isinstance()
By using isinstance() we can check if the object is instance of the class or not.