forked from astropy/astroquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_or_instance.py
More file actions
41 lines (31 loc) · 1010 Bytes
/
class_or_instance.py
File metadata and controls
41 lines (31 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Helper class that can be used to decorate instance
methods of a class so that they can be called either as a class method
or as instance methods.
"""
import functools
__all__ = ["class_or_instance"]
class class_or_instance:
def __init__(self, fn):
self.fn = fn
if hasattr(fn, '__doc__'):
self.__doc__ = fn.__doc__
else:
self.__doc__ = ""
def __get__(self, obj, cls):
def f(*args, **kwds):
if obj is not None:
return self.fn(obj, *args, **kwds)
else:
return self.fn(cls, *args, **kwds)
functools.update_wrapper(f, self.fn)
return f
class property_class_or_instance(property):
def __get__(self, obj, cls):
if obj is not None:
return self.fget(obj)
else:
return self.fget(cls)
def __set__(self, *args):
raise ValueError("Setters don't work.")