Python's GetAttr ()-June 7, 2017

Source: Internet
Author: User

The "1" getattr () function is Python the core function of introspection"2" Python's Introspection: Python can view functions or module information stored as objects in memory, as well as related operations. "3" Related functions: GetAttr (), SetAttr (), delattr (), hasattr () GetAttr related usages are described below: GetAttr is used to return an object property, or the method can be visually discerned by a demo:
classA:def __init__(self): SELF.A='a'     defmethod (self):Print "Method Print"a=A ()PrintGetAttr (A,'a','default')#If you have attribute a, print a, otherwise print the defaultPrintGetAttr (A,'b','default')#If you have attribute B, print B, otherwise print the defaultPrintGetAttr (A,'Method','default')   #If there is a method, print its address, otherwise print the defaultPrintGetAttr (A,'Method','default')()   #If there is a method, run the function and print none otherwise print default

Output

Adefault<bound method A.method of <__main__. A instance at 0x02cb7968>>method Printnone

Python's GetAttr ()-June 7, 2017

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.