Emulating classmethod in Python 2.1

F. GEIGER fgeiger at datec.at
Thu Jun 6 14:50:11 EDT 2002


There's a receipe about this in the ASPN, by Alex Martelli. Concise and
effective. Strongly recommended.

Cheers
Franz

"Hamish Lawson" <hamish_lawson at yahoo.co.uk> schrieb im Newsbeitrag
news:915a998f.0206060833.12ad76a8 at posting.google.com...
> 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