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 attribute doesn't
Exist; without it, an exception is raised in that case.
The explanation is very abstract. It tells me that this function is equivalent
Object. name
Tried getattr (object, name) and object. name is the same function. however, here we can use name as a variable to process the example in the book, which demonstrates the function of this function. Using getattr, we can easily implement the factory mode.
For example, a module supports printing in html, text, xml, and other formats. Based on the input formate parameter, different functions are called to implement output in several formats.
Copy codeThe Code is as follows:
Import statsout
Def output (data, format = "text "):
Output_function = getattr (statsout, "output _ % s" % format)
Return output_function (data)
[Code]
In this example, different methods of the statsout module can be called based on the format parameter of the input output function (output _ % s is implemented using formatted strings)
The returned object of this method can be directly used. If you want to add a new format, you only need to write a new method function in the module and use the new parameters when calling the output function, you can use different format output
Really convenient
To deepen understanding of the getattr function, repost an English description.
Python's getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute. In other words, the following two statements are equivalent:
[Code]
Value = obj. attribute
Value = getattr (obj, "attribute ")
If the attribute exists, the corresponding value is returned. If the attribute does not exist, you get an AttributeError exception instead.
The getattr function can be used on any object that supports dotted notation (by implementing the _ getattr _ method). This includes des class objects, modules, and even function objects.
Path = getattr (sys, "path ")
Doc = getattr (len, "_ doc __")
The getattr function uses the same lookup rules as ordinary attribute access, and you can use it both with ordinary attributes and methods:
Result = obj. method (args)
Func = getattr (obj, "method ")
Result = func (args)
Or, in one line:
Result = getattr (obj, "method") (args)
Calling both getattr and the method on the same line can make it hard to handle exceptions properly. to avoid confusing AttributeError exceptions raised by getattr with similar exceptions raised inside the method, you can use the following pattern:
Try:
Func = getattr (obj, "method ")
T AttributeError:
... Deal with missing method...
Else:
Result = func (args)
The function takes an optional default value, which is used if the attribute doesn't exist. The following example only cballs the method if it exists:
Func = getattr (obj, "method", None)
If func:
Func (args)
Here's a variation, which checks that the attribute is indeed a callable object before calling it.
Func = getattr (obj, "method", None)
If callable (func ):
Func (args)