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

Source: Internet
Author: User

Tags: return test std Default module MoS property attr Pytho

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.

Class Test ():

Name= "Xiaohua"

def run (self):

Return "Helloword"

T=test ()

Hasattr (T, "name") #判断对象有name属性

True

Hasattr (T, "Run") #判断对象有run方法

True

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 an object's method, the memory address of the method is returned, and if you need to run this method, you can add a pair of parentheses later.

Class Test ():

Name= "Xiaohua"

def run (self):

Return "Helloword"

T=test ()

GetAttr (T, "name") #获取name属性, the presence is printed out.

' Xiaohua '

GetAttr (T, "Run") #获取run方法, there is a memory address for printing out the method.

<bound method Test.run of <__main__.test instance at 0x0269c878>>

The GetAttr (T, "Run") () #获取run方法, followed by parentheses to run the method.

' Helloword '

GetAttr (T, "age") #获取一个不存在的属性.

Traceback (most recent):

File "<stdin>", line 1, in <module>

Attributeerror:test instance have no attribute ' age '

GetAttr (T, "age", "Max") #若属性不存在, returns a default value.

' 18 '

SetAttr (object, name, values)

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

Class Test ():

Name= "Xiaohua"

def run (self):

Return "Helloword"

T=test ()

Hasattr (T, "age") #判断属性是否存在

False

SetAttr (T, "age", "Max") #为属相赋值, and no return value

Hasattr (T, "age") #属性存在了

True

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

Class Test ():

Name= "Xiaohua"

def run (self):

Return "Helloword"

T=test ()

GetAttr (T, "age") #age属性不存在

Traceback (most recent):

File "<stdin>", line 1, in <module>

Attributeerror:test instance have no attribute ' age '

GetAttr (T, "age", SetAttr (T, "age", ")") #age属性不存在时, set this property

' 18 '

GetAttr (T, "age") #可检测设置成功

' 18 '

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.

Tags Index: