def __get__(self, instance, cls):
"""Descriptor protocol support.
This makes an instance of this class function correctly when it
is used to decorate a method on a user-defined class. If called
as a bound method, we store the decorated function in the instance
dictionary, so we will not be called again for that instance. If
called as an unbound method, we store a reference to the decorated
function internally and use it on future unbound method calls.
"""
if instance:
deco = instancemethod(self._decorated(cls, instance), instance, cls)
# This prevents us from being called again for this instance
setattr(instance, self._func.__name__, deco)
elif cls:
if not self.__clsfunc:
self.__clsfunc = instancemethod(self._decorated(cls), None, cls)
deco = self.__clsfunc
else:
raise ValueError("Must supply instance or class to descriptor.")
return deco
评论列表
文章目录