Go A detailed description of the GetAttr () function in Python

Source: Internet
Author: User

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

The explanation is very abstract and tells me that this function acts as a

Object.name

Try it. GetAttr (Object,name) is indeed the same function as Object.name. This is the only way to handle the name as a variable.

The example in the book is a good illustration of how this function functions.

Factory mode can be easily implemented with GetAttr.

Example: A module supports printing in HTML, text, XML and other formats, and calls different functions to implement output in several formats, depending on the formate parameter passed in.

Importdef output (data, format="text"):                               " output_%s " %     format"return output_function (data)

Return 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 when calling the output function, you can use a different format output

Reference link:http://www.cnblogs.com/pylemon/archive/2011/06/09/2076862.html

[GetAttr () function in]python

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.