Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created May 28, 2013 12:19
Show Gist options
  • Save astrofrog/5662363 to your computer and use it in GitHub Desktop.
Save astrofrog/5662363 to your computer and use it in GitHub Desktop.
@class_or_instance decorator
import functools
class class_or_instance(object):
def __init__(self, fn):
self.fn = fn
def __get__(self, obj, cls):
if obj is not None:
return lambda *args, **kwds: self.fn(obj, *args, **kwds)
else:
return lambda *args, **kwds: self.fn(cls, *args, **kwds)
class UKIDSS(object):
@class_or_instance
def query(self):
if self is UKIDSS:
print("Calling query as class method")
else:
print("Calling query as instance method")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment