码迷,mamicode.com
首页 > 编程语言 > 详细

Python内置函数(27)——hasattr

时间:2016-10-29 12:17:15      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:英文   desc   ini   err   his   没有   div   argument   asa   

英文文档:

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

  2. 函数实际上是调用getattr(object,name)函数,通过是否抛出AttributeError来判断是否含有属性。

Python内置函数(27)——hasattr

标签:英文   desc   ini   err   his   没有   div   argument   asa   

原文地址:http://www.cnblogs.com/sesshoumaru/p/6009654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!