Home  >  Article  >  Backend Development  >  Detailed introduction to Python’s built-in hasattr function

Detailed introduction to Python’s built-in hasattr function

高洛峰
高洛峰Original
2017-03-21 10:59:551405browse

English document:

  • 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.)


  • Instructions:

  •  1. The function is used to detect whether the object object contains a name named The attribute of name, if it exists, returns True, if not, returns 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

The above is the detailed content of Detailed introduction to Python’s built-in hasattr function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn