Skip to content

Instantly share code, notes, and snippets.

@keflavich
Forked from astrofrog/class_or_instance.py
Created June 18, 2013 14:59
Show Gist options
  • Save keflavich/5806078 to your computer and use it in GitHub Desktop.
Save keflavich/5806078 to your computer and use it in GitHub Desktop.
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