def __get__(self, obj, type=None):
# raise an AttributeError if the attribute is not present on the object
if obj is not None:
# delegate only on instances, not the classes.
# this is to allow access to the docstrings.
for delegate_name in self.delegate_names:
try:
delegate = getattr(obj, delegate_name)
except AttributeError:
continue
else:
if not isinstance(delegate, self.instance_type):
raise TypeError('delegate (%s) is not an instance of %s'
% (delegate, self.instance_type))
break
else:
attrgetter(self.delegate_names[-1])(obj)
# lambda, but not partial, allows help() to work with update_wrapper
out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)
# update the docstring of the returned function
update_wrapper(out, self.fn)
return out
评论列表
文章目录