Detailed explanation of the usage of Python's hasattr() getattr() setattr() function



hasattr(object, name) determines whether an object has a name attribute or a name method, returns a BOOL value, and returns True if there is a name attribute, otherwise returns False. It should be noted that the name should be enclosed in parentheses

 

  PYTHON;gutter:true;">>>> class test():

... name="xiaohua"

... def run(self):

. .. return "HelloWord"

...

>>> t=test()

>>> hasattr(t, "name") #Judging that the object has a name attribute

True

>>> hasattr (t, "run") #Judging that the object has a run method

True

>>>



 

getattr(object, name[,default]) Get the attributes or methods of the object object, if it exists, print it out, if it does not exist, print it out Default value, the default value is optional. It should be noted that if it is a method of the returned object, the memory address of the method is returned. If you need to run the method, you can add a pair of parentheses after it.

 

  PYTHON;gutter:true;">&



... def run(self):

... return "HelloWord"

...

>>> t=test()

>>> getattr(t, "name") #Get the name attribute , print it if it exists.

'xiaohua'

>>> getattr(t, "run") #Get the run method, print out the memory address of the method if it exists.

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

>>> getattr(t, "run")() #Get the run method, followed by parentheses to run this method.

'HelloWord'

>>> getattr(t, "age") #Get an attribute that does not exist.

Traceback (most recent call last):

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



>>> getattr(t, "age","18") #If the attribute does not exist, return a default value.

'18'

>>>



 

 setattr(object, name, values) assigns an attribute to the object. If the attribute does not exist, create it first and then assign it.

 

  PYTHON;gutter:true;">>>> class test():

... name="xiaohua"

... def run(self):

... return "HelloWord"

...

>>> t=test()

>>> hasattr(t, "age") #Determine whether the attribute exists

False

>>> setattr(t, "age", "18" ;) #Assign the zodiac sign, and no return value

>>> hasattr(t, "age"







 



 

  PYTHON;gutter:true;">>>> class test():

...     name="xiaohua"

...     def run(self):

...             return "HelloWord"

...

>>> t=test()

>>> getattr(t, "age")    #age属性不存在

Traceback (most recent call last):

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

AttributeError: test instance has no attribute 'age'

>>> getattr(t, "age", setattr(t, "age", "18")) #age属性不存在时,设置该属性

'18'

>>> getattr(t, "age") #Detectable setting is successful >>>

'18'





 

 

Author: Cen Yu Source: http://www.cnblogs.com/cenyu/ The copyright of this article belongs to the author and the blog garden. Reprints are welcome, but this statement must be retained without the author's consent, and the original text should be given in an obvious position on the article page. Connect, otherwise reserve the right to pursue legal responsibility. If there are any mistakes in the text, please point them out. lest more people be misled. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326367833&siteId=291194637