![图片[1]-python判断对象是否有某个属性-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5d69e2820987b852.jpg)
hasattr(object, name)
说明:判断对象object是否包含名为name的属性(hasattr是通过调用getattr(ojbect, name)是否抛出异常来实现的)。
参数object:对象。
参数name:属性名称。
返回为True或者为False
不过自己也可以定义一个方法
defgetattribute(self,name): try: r=object.__getattribute__(self,name) except: r=None returnr
通过返回值来判断!
有hasattr就更方便了!
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容