Python advanced (4) getattr usage

Source: Internet
Author: User

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.
In addition, this function is often used in callback functions, which is not very well understood.

Take a look at the official documentation:

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's attributes, the result is the value of that attribute. for example, getattr (x, 'foobar') is equivalent to x. foobar. if the named attribute does not exist, defaultis returned if provided, otherwise AttributeError is raised.

Parameter description:

    Object: Instance name of the object: String, member function name of the object or member variable default: if the object does not have this attribute, the default value returned is abnormal: if this attribute does not exist and there is no default return value, "AttrbuteError" is thrown"

    Purpose:

    Getattr (object, name) = object. name

    Demo

    <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> result = obj. method (args) // use getattrfunc = getattr (obj, "method") result = func (args) // or write a row of result = getattr (obj, "method ") (args)


    Abnormal security statement:

    There are two main exceptions:

    AttributeError: This attribute is not found in the object.

    Try: func = getattr (obj, "method") failed t AttributeError :...... dealelse: result = func (args) // or specify the default return value func = getattr (obj, "method", None) if func: func (args)

    TypeError: cannot be called

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

    Use getattr to implement the factory method:

    Demo: 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.

    import statsout def output(data, format="text"):                               output_function = getattr(statsout, "output_%s" %format)     return output_function(data)

    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)


    References:

    Http://www.cnblogs.com/pylemon/archive/2011/06/09/2076862.html Python find the getattr () function details

    Http://docs.python.org/2.7/library/functions.html? Highlight = getattr # getattr python2.7 documentation

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.