求助,这里cls是None吗?
本人小白,请帮忙看下这段代码,我想问下,最后d.__doc__执行的结果为什么没有打印"旺财"呢?self是__doc__,obj是d,cls应该没有传入吧,所以应该满足"cls is None"这个条件吧?是我理解有问题吗?请大神指教!class MethodType:
def __init__(self,func,obj):
self.__func__=func
self.__self__=obj
def __call__(self,*args,**kwargs):
func=self.__func__
obj=self.__self__
print("小白")
return func(obj,*args,**kwargs)
class ClassMethod:
def __init__(self,f):
self.f=f
def __get__(self,obj,cls=None):
if cls is None:
print("旺财")
cls=type(obj)
if hasattr(type(self.f),'__get__'):
print(f"来福,type(self.f)->{type(self.f)}")
return self.f.__get__(cls,cls)
return MethodType(self.f,cls)
class D:
@ClassMethod
@property
def __doc__(cls):
return f"I love FishC.--from class {cls.__name__}"
d=D()
d.__doc__
来福,type(self.f)-><class 'property'>
'I love FishC.--from class D'