The GetAttr function in Python uses the method GetAttr implements the factory pattern _python

Source: Internet
Author: User
Tags in python

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 returned the attribute doesn ' t
Exist Without it, an exception was raised in the case.

The explanation is very abstract and tells me that this function is equivalent to

Object.name

Try it. GetAttr (Object,name) is indeed the same function as object.name. It's just that the name can be used as a variable to deal with the example of the book is a good illustration of the function of the functions, the use of GetAttr can easily implement the factory model.

Example: A module to support the HTML, text, XML and other formats of printing, according to the incoming formate parameters of different, call different functions to achieve several formats of output

Copy Code code as follows:

Import Statsout
def output (data, format= "text"):
Output_function = GetAttr (statsout, "output_%s"%format)
return Output_function (data)
[Code]
In this example, you can invoke different methods of the Statsout module based on the different format parameters of the incoming output function (using the formatted string to implement output_%s)

Returns the object of this method can be used directly if you want to add a new format only need to write a new method function in the module to use the new parameters when calling the output function can use a different format output

It's really convenient.


In order to deepen the understanding of the GetAttr function, reprint an English explanation

Python ' s getattr function is used to fetch the attribute from the object, using a string object instead of a identifier to Identify the attribute. In the 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 have a Attributeerror exception instead.

The GetAttr function can be used to the any object this 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 can use it both with ordinary attrib Utes 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 the same line can make it hard to handle exceptions. To avoid confusing attributeerror exceptions raised through GetAttr with similar exceptions raised the method, can u Se the following pattern:

Try
Func = getattr (obj, "method")
Except Attributeerror:
... deal with missing ...
Else
result = Func (args)
The function takes an optional default value and which is used if the attribute doesn ' t exist. The following example only calls the if it exists:

Func = getattr (obj, "method", None)
If Func:
Func (args)
Here's a variation, which checks that's the attribute is indeed a callable object before it.

Func = getattr (obj, "method", None)
If callable (func):
Func (args)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.