Python's hasattr () GetAttr () setattr () function uses a detailed approach

Source: Internet
Author: User
Tags stdin

Hasattr (object, name)
Determines whether an object has a Name property or a name method, returns a bool value, returns true with the name attribute, or FALSE.
It is important to note that name is enclosed in parentheses.

1 >>> class Test (): 2     ... Name= "Xiaohua" 3     ... def run (self): 4             ... Return "Helloword" 5 ... 6 >>> t=test () 7 >>> hasattr (T, "name") #判断对象有name属性 8 True 9 >>> hasattr (T, "Run")  #判断对象有r Un method True11 >>>

GetAttr (object, Name[,default])
Gets the object's property or method, if it exists, prints the default value if it does not exist, and the default value is optional.
It is important to note that if you are returning the method of the object, the memory address of the method is returned, if you need to run this method,
You can add a pair of parentheses later.

1 >>> class Test (): 2     ... Name= "Xiaohua" 3     ... def run (self): 4             ... Return "Helloword" 5 ... 6 >>> t=test () 7 >>> getattr (T, "name") #获取name属性, the presence is printed out. 8 ' Xiaohua ' 9 >>> getattr (T, "Run")  #获取run方法, there is a memory address for printing out the method. <bound method Test.run of <__main__.test instance at 0x0269c878>>11 >>> getattr (t, "Run") ()  # Gets the Run method, which is followed by parentheses to run the method. The ' Helloword ' >>> getattr (t, "age")  #获取一个不存在的属性. Traceback (recent):   "<stdin>", line 1, <module>16 attributeerror:test Instanc e have no attribute ' age ' >>> getattr (t, "age", "Max")  #若属性不存在, returns a default value. @ >>>

SetAttr (object, name, values)
Assigns a value to the object's property and, if the property does not exist, first creates a re-assignment.

1 >>> class Test (): 2     ... Name= "Xiaohua" 3     ... def run (self): 4             ... Return "Helloword" 5 ... 6 >>> t=test () 7 >>> hasattr (T, "age")   #判断属性是否存在 8 False 9 >>> setattr (T, "age", "+")   #为属相赋值, and there is no return value of >>> hasattr (T, "age")    #属性存在了11 True12 >>>

A comprehensive usage is to determine whether an object's properties exist and add the property if it does not exist.

1 >>> class Test (): 2     ... Name= "Xiaohua" 3     ... def run (self): 4             ... Return "Helloword" 5 ... 6 >>> t=test () 7 >>> getattr (T, "age")    #age属性不存在 8 Traceback (most recent call last): 9   File "&L T;stdin> ", line 1, in <module>10 attributeerror:test instance have no attribute ' age ' >>> getattr (t," Age ", SetAttr (T," Age "," (")") #age属性不存在时, set this property to >>> getattr (T, "age")  #可检测设置成功14 ' + ' >> >

Python's hasattr () GetAttr () setattr () function uses a detailed approach

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.