首页 > 编程语言 > 详细

Python内置函数(51)——hasattr

时间:2017-12-29 16:50:52      阅读:187      评论:0      收藏:0      [点我收藏+]

英文文档:

hasattr(objectname)
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 AttributeErroror 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内置函数(51)——hasattr

原文:https://www.cnblogs.com/lincappu/p/8145124.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!