Class methods that do not require an object instance.
1 2 3 4 5 6 7 | >>> class Class:
... class StaticFudge:
... def __call__(self): print "I do not require an instance."
... classmethod = StaticFudge()
...
>>> Class.classmethod()
I do not require an instance.
|
Tags: oop
Where is the discussion? Very nice approach! Please do not omit the discussion. If this is supposed to help people with intermediate python knowledge you should be more specific about when and why the problem you're trying to solve occurs and what's your paricular trick for solving the problem?
static methods and classmethods in python 2.2 and later. Python 2.2 and later support static methods by using the built in staticmethod function. It looks like an ugly hack, but it works.
From the python documentation:
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.
In addition to static methods, python also has classmethods:
From the python documentation:
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.