Section 13.8. Static Methods and Class Methods


13.8. Static Methods and Class Methods

Static methods and class methods were introduced in Python 2.2. They can be used with both classic classes and new-style classes. A pair of built-in functions were added to "tag," "cast," or "convert" methods declared as part of class definitions as either one of these two types of methods.

Static methods are exactly what they are if you are coming from C++ or Java. They are simply functions (no instance required) that are part of class definitions. In fact, before static methods were added to Python, users just created functions in the global namespace as a proxy for this missing featuresometimes using a class object inside such functions to manipulate the class (or rather, class attributes). Using module functions is still far more common than using static class methods.

Recall that regular methods require an instance (self) as the first argument, and upon (bound) method invocation, self is automagically passed to the method. Well, for class methods, instead of the instance, the class is required as the first argument, and it is passed in to the method by the interpreter. The class does not need to be specifically named like self, but most people use cls as the variable name.

13.8.1. staticmethod() and classmethod() Built-in Functions

Now let us look at some examples of these types of methods using classic classes (you can also use new-style classes if you want to):

 class TestStaticMethod:      def foo():          print 'calling static method foo()'          foo = staticmethod(foo) class TestClassMethod:      def foo(cls):          print 'calling class method foo()'          print 'foo() is part of class:', cls.__name__      foo = classmethod(foo)


The corresponding built-in functions are converted into their respective types and are reassigned back to the same variable name. Without the function calls, both would generate errors from the Python compiler, which is expecting regular method declarations with self. We can then call these functions from either the class or an instance... it makes no difference:

    >>> tsm = TestStaticMethod()     >>> TestStaticMethod.foo()     calling static method foo()     >>> tsm.foo()     calling static method foo()     >>>     >>> tcm = TestClassMethod()     >>> TestClassMethod.foo()     calling class method foo()     foo() is part of class: TestClassMethod     >>> tcm.foo()     calling class method foo()     foo() is part of class: TestClassMethod


13.8.2. Using Decorators

Now, seeing code like foo = staticmethod(foo) can irritate some programmers. There is something unsettling about it, and many folks were upset with such a flimsy syntax, although van Rossum had pointed out that it was to be temporary until the semantics were worked out with the community. In Section 11.3.6 of Chapter 11, "Functions," we looked at decorators, a new feature introduced in Python 2.4. They are used in places where you want to apply a function to a function object but want to rebind the new function object to the original variable. This is a perfect place to use them to partially clean up the syntax. By using decorators, we can avoid the reassignment above:

class TestStaticMethod:     @staticmethod     def foo():         print 'calling static method foo()' class TestClassMethod:     @classmethod     def foo(cls):         print 'calling class method foo()'         print 'foo() is part of class:', cls.__name__




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net