Thursday, December 9, 2010

how built-in staticmethod (could be) implemented

>>> class MyStatic(object):
...    def __init__(self, f): self.f = f
...    def __get__(self, obj, type=None): return self.f
...
>>> class C(object):
...    @MyStatic
...    def foo(): print "Hello World!"
...
>>> C.foo()
Hello World!

No comments:

Post a Comment