classmethod()

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()
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.
staticmethod()

Using isinstance()

By using isinstance() we can check if the object is instance of the class or not.

All Built in Functions in Python locals() dir() ord()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer