Static Methods in Python

From PeformIQ Upgrade
Jump to navigation Jump to search

Hunting around found this example - http://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/

## {{{ http://code.activestate.com/recipes/52304/ (r3)
# direct, naive approach -- doesn't work...:
class Class1:
    def static1(name):
        print "Hello",name

# ...but now, a call such as:
Class1.static1("John")
# will fail with a TypeError, as 'static1' has become 
# an unbound-method object, not a plain function.

# This is easy to solve with a simple tiny wrapper:
class Callable:
    def __init__(self, anycallable):
        self.__call__ = anycallable

# toy-example usage:
class Class2:
    def static2(name):
        print "Hi there",name
    static2 = Callable(static2)

# now, a call such as:
Class2.static2("Peter")
# works just fine, and as-expected
## end of http://code.activestate.com/recipes/52304/ }}}