Net-informations.com
SiteMap  | About    

Class method Vs Static method

Python @classmethod means that when this method is called, we pass the class as the first argument instead of the instance of that class ("self"). @classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance, can be overridden by subclass. That's because the first argument for @classmethod function must always be cls (class). Also, @classmethod is important when you want to write a factory method and by this custom attribute(s) can be attached in a class. This attribute(s) can be overridden in the inherited class.

A staticmethod is a method that knows nothing about the class or instance it was called on. @staticmethod means that when this method is called, we don't pass an instance of the class to it. This means that, there is no need to pass implicit argument like self or cls. It is callable without instantiating the class first. It's definition is immutable via inheritance. It is basically useless in Python because you can just use a module function instead of a staticmethod.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.