Emulating classmethod in Python 2.1

Hamish Lawson hamish_lawson at yahoo.co.uk
Thu Jun 6 12:33:54 EDT 2002


Is it possible to define a 'classmethod' class or function for Python
2.1 that would effectively provide the functionality of Python 2.2's
built-in classmethod function and have the same usage pattern? I'd
like to be able run code like that below against both 2.2 (using its
builtin classmethod function) and 2.1 (using some added-in wrapper).

    class SomeClass:
        def hello(cls, name):
            print "Hi there", cls, name
        hello = classmethod(hello)

    SomeClass.hello("Peter")

I've come across Thomas Heller's recipe in the online Python Cookbook,
but this would require SomeClass to be modified, and so fails my
criteria.

I've also come across Alex Martelli's recipe for emulating
staticmethod, but of course that doesn't deal with passing a class to
the wrapped method.


Hamish Lawson



More information about the Python-list mailing list