Skip to content

Instantly share code, notes, and snippets.

@ptsurko
Created April 19, 2015 10:27
Show Gist options
  • Save ptsurko/97cc52d6eb75ddddfbd8 to your computer and use it in GitHub Desktop.
Save ptsurko/97cc52d6eb75ddddfbd8 to your computer and use it in GitHub Desktop.
Python descriptors
class classmethod(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, klass):
if klass is None:
klass = type(instance)
def newfunc(*args, **kwargs):
return self.func(klass, *args, **kwargs)
return newfunc
def __call__(self, func):
return classmethod(func)
class staticmethod(object):
def __init__(self, func):
self.func = func
def __get__(self, *_):
return self.func
def __call__(self, func):
return staticmethod(func)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment