This article mainly introduces some usage of the getattr () function in python. For more information, see the doc of the function.
The code is as follows:
Getattr (object, name [, default])-> value
Get a named attribute from an object; getattr (x,
Read the doc of the function itself.Copy codeThe Code is as follows:Getattr (object, name [, default])-> value
Get a named attribute from an object; getattr (x, 'y') is equivalent to x. y.When a default argument is given, it is returned when the
Look at the next function itself doc
Copy Code code as follows:
GetAttr (object, name[, default])-> value
Get a named attributes from object; GetAttr (x, ' Y ') is equivalent to x.y.When a default argument are given, it is
Class-Built-in functions
Services to classes and objects.
9 Built-in functions
Issubclass,isinstance,hasattr,getattr,setattr,delattr,dir,super,vars
Note: GetAttr () and delattr () may throw exceptions (such as Attributeerror) to handle exceptions
Introduction and example of the built-in function getattr () in python, pythongetattr
In the official python documentation: getattr () is explained as follows:
getattr(object, name[, default])Return the value of the named attribute of object. name
The difference between _ get _ and _ getattr _ and _ getattribute _ in Python, __get ___ getattr _
IntroductionSuppose we have A class a, where A is an instance of.What happened in a. x? The lookup sequence of attributes is as follows:
If _
Python _ setattr _, _ getattr _, _ delattr _, _ call _ usage example, pythongetattr
Getattr
The 'getattr' function is a built-in function and can be obtained by function name.Copy codeThe Code is as follows:Value = obj. attributeValue = getattr (obj,
The GetAttr () function in Python is detailed: GetAttr (object, name[, default]), value Get A named attribute from an object; GetAttr (x, ' Y ') is equivalent to x.y. When a default argument is given, it's returned when the attribute doesn ' t
This article mainly introduces the usage examples of Python _ setattr _, _ getattr _, _ delattr _, and _ call, this article explains these magic methods separately. For more information, see
Getattr
The 'getattr' function is a built-in function and
Problem focus:
If you are familiar with getattr, you should know that getattr (a, 'B') serves the same purpose as a. B.
So what is the role of this built-in function? The most convenient thing is to use it to implement the Factory Method mode.
Analysis of getattr (), setattr (), delattr (), hasattr (), and getattrsetattr in Python
Getattr () functionIs the core function of Python introspection. The usage is as follows:
Get object reference getattr
Getattr is used to return an object
In fact, the main function of the getattr () method is to implement the reflection mechanism. That is to say, you can use strings to obtain the method instance. In this way, you can place the methods that a class may call in the configuration file
Help on built-in function getattr in module __builtin__:GetAttr (...)GetAttr (object, name[, default]), valueGet A named attribute from an object; GetAttr (x, ' Y ') is equivalent to x.y.When a default argument is given, it's returned when the
The GetAttr () function is the core function of Python introspection, which is used in the following general context:Get Object reference GetAttrGetAttr is used to return an object property, or a method
class A:
def __init__ (self):
In the official Python document: GetAttr () is interpreted as follows:?
123
getattr(object, name[, default])Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object‘
in Python programming (http://www.maiziedu.com/course/python-px/, most programmers agree that there is little need to focus on the details of GetAttribute and GetAttr, and in general, when consuming our custom classes, we understand the structure of
Hasattr (object, name)Determines if an object (object) has a Name property or method that returns a Boolean value that has the Name property return True, otherwise falseIn [1]: Class Test (object): ...: name = ' hkey ' ...: def hello
In the official Python document: GetAttr () is explained as follows:
GetAttr (object, name[, default]) return the value of the
named attribute of object. Name must to be a string. If The string is the name of one of the object's attributes,
In the official Python document: GetAttr () is interpreted as follows:
GetAttr (object, name[, default]) Return the value of the named attribute of object. Name must is a string. The If the string is the name of the one of the object ' s attributes,
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.