@staticmethod in python

Home Forums Python Programming @staticmethod in python

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2061 Reply
    Humble
    Keymaster

    @staticmethod is a decorator whcih make a class method to be static . When I say “static method”, it means, the subjected method is not bound to any instance of the class or the class itself.

    There is no relation to the method and the class or instance where it is defined.. Normally, the class methods expect the first argument to be “self” when its invoked from any object instance. how-ever, static methods in a class does not need this argument.

    In short, staticmethod is a function inside a class. You can call it without creating instance of the class. Inheritance dont have any effect on static methods..

    To create a static method you need to decorate the method with a keyword called, @staticmethod:

    >>> class Company:
    ...     @staticmethod
    ...     def static_decorator_func():
    ...             print "I am static method"
    ... 
    >>> 
    >>> Company.static_decorator_func()
    I am static method
    >>> 
    
    
    Please let me know if you have any doubt..
    
Viewing 1 post (of 1 total)
Reply To: @staticmethod in python
Your information: