Skip to content

Instantly share code, notes, and snippets.

@yosisa
Created March 24, 2011 13:58
Show Gist options
  • Save yosisa/885099 to your computer and use it in GitHub Desktop.
Save yosisa/885099 to your computer and use it in GitHub Desktop.
import new
class myclassmethod(object):
def __init__(self, func):
self.func = func
self._first = True
def __get__(self, instance, klass):
if self._first:
self.func = new.instancemethod(self.func, klass, type)
self._first = False
return self.func
class Foo(object):
value = 0
@classmethod
def foo(cls):
cls.value += 1
return cls.value
@myclassmethod
def bar(cls):
cls.value += 1
return cls.value
if __name__ == '__main__':
print dir(Foo.foo) == dir(Foo.bar)
print Foo.foo
print Foo().foo
print Foo.bar
print Foo().bar
print Foo.foo()
print Foo().foo()
print Foo.bar()
print Foo().bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment