The built-in function getattr () in python ()

Source: Internet
Author: User

The built-in function getattr () in python ()
In the official python documentation: getattr () is interpreted as follows: 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, default is returned if provided, otherwise AttributeError is Raised. returns the object Value Based on the attribute name. If "name" is the name of the Object Property, the value of the corresponding property is returned. Copy the code and copy the code '#-*-coding: UTF-8-*-' _ author _ = 'Lucas 'class attrtest (object): def _ init _ (self): pass def trygetattr0 (self): self. name = 'Raw' print self. name # equals to self. name print getattr (self, 'name') def attribute1 (self, para1): print 'bubute1 called and '+ para1 +' is passed in as a parameter 'def trygetattr (self): fun = getattr (self, 'bubute1') print type (fun) fun ('crown ') if _ nam E __= '_ main _': test = attrtest () print 'getattr (self, \ 'name \ ') equals to self. name 'test. trygetattr0 () print 'bubute1 is indirectly called by fun () 'test. trygetattr () print 'attrbucte1 is directly called' test. attribute1 ('tomato') copies the code and copies the code. The result of this code execution is: copy the code getattr (self, 'name') equals to self. name lucaslucasattribute1 is indirectly called by fun () <type 'instancemethod'> attribute1 called and cr Own is passed in as a parameterattrribute1 is directly calledattribute1 called and tomato is passed in as a parameter Process finished with exit code 0 copy the code first function tryattribute0 () very easy to understand, as defined in the definition. The second function tryattribute1 () is a bit confusing. In fact, the principle is not complex. We can see that the type of fun is instancemethod. Here you can think that for a function, the return value of getattr () is a pointer, And the pointer is assigned to the variable that accepts it, in the future, call is equivalent to calling the function pointed to by the variable. Now that we know the principle, what is the role of getattr? Are you familiar with reflection in java or c? One important function of reflection is delayed loading, which can be decoupled to make the system run more efficiently. As a dynamic language, python is obviously more powerful in this aspect. getattr () is a building block for implementing python reflection. It can be combined with other methods such as setattr () and dir, we can do a lot of interesting things. Let's look at the following scenarios: 1. I need to dynamically add methods in other classes in one class: copy the Code # If Class A has the following method: def addnewattributesfromotherclass (self, class_name): func_names = dir (class_name) for func_name in func_names: if not func_name.startswith ('_'): new_func = getattr (class_name, func_name) self. _ setattr _ (func_name, new_func () to copy the code, we only need to: a = A () B = B (). addnewattributesfromotherclass (B) so that a can call the 'non-private' method in B.

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.