Skip to content

Instantly share code, notes, and snippets.

@MBrouns
Last active March 16, 2019 16:02
Show Gist options
  • Save MBrouns/ddb6019c68bfd93061b1b0c6d4af85e7 to your computer and use it in GitHub Desktop.
Save MBrouns/ddb6019c68bfd93061b1b0c6d4af85e7 to your computer and use it in GitHub Desktop.
class classmethod_:
"""Allows using a classmethod also as an instance method"""
def __init__(self, cls_method, inst_method=None):
self.cls_method = cls_method
self.inst_method = inst_method
def __get__(self, obj, objtype=None):
if obj is not None:
return lambda *args: self.inst_method(obj, *args)
if objtype is None:
objtype = type(obj)
return lambda *args: self.cls_method(objtype, *args)
def instance_method(self, inst_method):
return type(self)(self.cls_method, inst_method)
class MyClass:
@ClassMethod
def fromkeys(cls):
print('cls', cls)
@fromkeys.instance_method
def fromkeys(self):
print('self', self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment