Python内置hasattr函数的详细介绍
程序员文章站
2022-04-04 23:24:55
...
英文文档:
hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)
说明:
1. 函数功能用来检测对象object中是否含有名为name的属性,如果有则返回True,如果没有返回False
#定义类A >>> class Student: def __init__(self,name): self.name = name >>> s = Student('Aim') >>> hasattr(s,'name') #a含有name属性 True >>> hasattr(s,'age') #a不含有age属性 False
以上就是Python内置hasattr函数的详细介绍的详细内容,更多请关注其它相关文章!
上一篇: mysql怎么导入excel数据?
下一篇: MySQL联合索引用法示例_MySQL