0001__all__ = ['classinstancemethod']
0002
0003class classinstancemethod(object):
0004    """
0005    Acts like a class method when called from a class, like an
0006    instance method when called by an instance.  The method should
0007    take two arguments, 'self' and 'cls'; one of these will be None
0008    depending on how the method was called.
0009    """
0010
0011    def __init__(self, func):
0012        self.func = func
0013
0014    def __get__(self, obj, type=None):
0015        return _methodwrapper(self.func, obj=obj, type=type)
0016
0017class _methodwrapper(object):
0018
0019    def __init__(self, func, obj, type):
0020        self.func = func
0021        self.obj = obj
0022        self.type = type
0023
0024    def __call__(self, *args, **kw):
0025        assert not kw.has_key('self') and not kw.has_key('cls'), (
0026            "You cannot use 'self' or 'cls' arguments to a "
0027            "classinstancemethod")
0028        return self.func(*((self.obj, self.type) + args), **kw)
0029
0030    def __repr__(self):
0031        if self.obj is None:
0032            return ('<bound class method %s.%s>'
0033                    % (self.type.__name__, self.func.func_name))
0034        else:
0035            return ('<bound method %s.%s of %r>'
0036                    % (self.type.__name__, self.func.func_name,
0037                       self.obj))