In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Python hasattr (), getattr (), setattr () function how to use", detailed content, clear steps, details handled properly, I hope that this "Python hasattr (), getattr (), setattr () function how to use" the article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.
Hasattr ()
The hasattr () function is used to determine whether a class instance object contains a property or method with the specified name.
The syntax format of this function is as follows:
Hasattr (obj, name)
Where obj refers to the instance object of a class, name represents the specified property name or method name, returns Bool value, returns True with name attribute, otherwise returns False.
Example:
Class demo: def _ init__ (self): self.name = "lily" def say (self): print ("say hi") d = demo () print (hasattr (d, 'name')) print (hasattr (d,' say') print (hasattr (d, 'eat'))
The running results are as follows:
True
True
False
Getattr ()
The getattr () function gets the value of the specified property in an instance object of a class.
The syntax format of this function is as follows:
Getattr (object, name [, default])
Where obj represents the specified class instance object, name represents the specified attribute name, and default is an optional parameter, which is used to set the default return value of the function, that is, when the function lookup fails, if you do not specify the default parameter, the program will directly report the AttributeError error, otherwise the function will return the value specified by default.
Example:
Class demo: def _ init__ (self): self.name = "lily" def say (self): return "say hi" d = demo () print (getattr (d, 'name')) print (getattr (d,' say')) print (getattr (d, 'eat'))
The running results are as follows:
Lily
Traceback (most recent call last):
File "/ test.py", line 11, in
Print (getattr (d, 'eat'))
AttributeError: 'demo' object has no attribute' eat'
As you can see, getattr () returns the value of an existing property in the class, and the status information of the method if the name is a method name; conversely, if the understanding is not owned by the class object, either the default parameter is returned, or the program reports an AttributeError error.
It is important to note that if it is the method of the returned object, it returns the memory address of the method, and if you need to run this method, you can add a pair of parentheses after it. For example:
Class demo: def _ init__ (self): self.name = "lily" def say (self): return "say hi" def eat (self, something): return f "eat {something}" d = demo () print (getattr (d, 'name') print (getattr (d,' say')) print (getattr (d, 'eat') (' apple')) print (getattr (d, 'eat') 'no eat') (' banana'))
The running results are as follows:
Lily eat apple eat banana
Setattr ()
The most basic function of the setattr () function is to modify the property values in the class instance object. Second, it can also be implemented to dynamically add properties or methods to the instance object.
The syntax format of this function is as follows:
Setattr (obj, name, value)
Example:
Class demo: def _ init__ (self): self.name = "lily" d = demo () print (getattr (d, 'name')) print (' -') setattr (d, 'name',' tom') print (getattr (d, 'name')) print (' -') print (hasattr (d, 'age') setattr (d,' age', '18') print (hasattr (d) 'age')) print (getattr (d,' age'))
The running results are as follows:
Lily
-
Tom
-
False
True
eighteen
Read this, the "Python hasattr (), getattr (), setattr () function how to use" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 210
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.