Detailed introduction to Python's built-in hasattr function

高洛峰
Release: 2017-03-21 10:59:55
Original
1487 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template