The getattr function in python implements the factory pattern using the method getattr
- 2020-04-02 13:22:33
- OfStack
I looked at the doc of the function itself
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.
It's very abstract and it tells me that this function does exactly the same thing
Object. The name
It is true that getattr(object,name) is the same function as object.name, except that you can use the name as a variable to handle the example in the book, which well illustrates the function's function.
Example: a module supports printing in HTML, text, XML, etc. Depending on the parameters of the formate passed in, different functions are called to achieve output in several formats
import statsout
def output(data, format="text"):
output_function = getattr(statsout, "output_%s" %format)
return output_function(data)
[code]
In this example, you can pass in output Function of the format Parameter difference To call statsout Module different methods ( Implemented as a formatted string output_%s)
Returns the object of this method You can use it directly If you want to add a new format You just need to write the new method function in the module In the call output A function that takes a new parameter can be output in a different format
It's really convenient
In order to deepen the relationship getattr Understanding of function Reprint a note in English
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 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")
except 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 calls 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)