2019年8月18日 回顾

# import test1 as obj#将tes同重命名为obj,并导入
# print(hasattr(obj,'name'))

import  sys
obj=sys.modules[__name__] #__name__就是当前文件名


print(hasattr(obj,' x'))
class Foo:
    x=1
    def __getattribute__(self, item):#不管是否能找到都会运行
        print('执行的是getattribute')
        raise AttributeError('抛出异常')#抛出异常后,getattr才会去运行

    def __getattr__(self, item):
        print('执行getattr')


f1=Foo()

print(isinstance(f1,Foo))#判断f1是否是Foo实例而来

class Bar(Foo):
    pass

print(issubclass(Bar,Foo))#判断bar是否为foo的子类

print(f1.x)

》》》》》

True
True
执行的是getattribute
执行getattr
None

猜你喜欢

转载自www.cnblogs.com/python1988/p/11371324.html