Commits

Jonathan Eunice  committed 1a15869

Added combomethod to repo.

  • Participants
  • Parent commits 0e95dfc

Comments (0)

Files changed (1)

File options/combomethod.py

+"""
+Decorator that allows a method to be both a class method
+and an instance method at the same time.
+"""
+
+# Yet again I am indebted to Stack Overflow
+# http://stackoverflow.com/questions/2589690/creating-a-method-that-is-simultaneously-an-instance-and-class-method
+
+import functools
+
+class combomethod(object):
+    def __init__(self, method):
+        self.method = method
+
+    def __get__(self, obj=None, objtype=None):
+        @functools.wraps(self.method)
+        def _wrapper(*args, **kwargs):
+            if obj is not None:
+                return self.method(obj, *args, **kwargs)
+            else:
+                return self.method(objtype, *args, **kwargs)
+        return _wrapper
+