首頁  >  文章  >  後端開發  >  Python內建hasattr函數的詳細介紹

Python內建hasattr函數的詳細介紹

高洛峰
高洛峰原創
2017-03-21 10:59:551406瀏覽

英文文件:

  • 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 

    #Erroroo


  • #說明:

  • #  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函數的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn