Python reflection, built-in functions (__str__, __repr__)

Source: Internet
Author: User

First, reflection

Manipulate object-related properties in the form of strings. (Use the variable name of the string data type to get the value of the variable)

All things in Python are objects (you can use reflection)

#hasattrdefHasattr (*args, **kwargs):#Real Signature Unknown    """Return whether the object has a attribute with the given name.    This is do by calling GetAttr (obj, name) and catching Attributeerror. """    Pass#GetAttrdefGetAttr (object, Name, Default=none):#known special case of GetAttr    """GetAttr (object, name[, default]), value Get a named attribute from an object; GetAttr (x, ' Y ') is equiv    Alent 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. """    Pass#SetAttrdefSetAttr (x, Y, v):#real signature unknown; restored from __doc__    """sets the named attribute on the given object to the specified value. SetAttr (x, ' Y ', V) is equivalent to ' x.y = V '"""    Pass#delattrdefDelattr (x, y):#real signature unknown; restored from __doc__    """deletes the named attribute from the given object. Delattr (x, ' Y ') is equivalent to ' del x.y '"""    Pass
4 ways to reflect the source code

  

#variables in reflection classes: Static Properties, class methods, static methodsclassFoo:school='Oldboy'Country=' China'language='chiness'@classmethoddefClass_method (CLS):Print(cls.school) @staticmethoddefStatic_method ():Print('In Staticmethod')    defWahaha (self):Print('Wahaha')Print(Foo.school)#OldboyPrint(Foo.country)# ChinaPrint(Foo.language)#chiness#Judgment ImplementationINP = input ('>>>')ifINP = ='School':Print(Foo.school)#OldboyelifINP = ='Country':Print(Foo.country)# ChinaelifINP = ='language':Print(Foo.language)#chiness#Reflection Implementation while1: InP= Input ('>>>')#Output School, print Oldboy country, print the China output language, print chiness    Print(GetAttr (FOO,INP))#Analytic GetAttr MethodPrint(GetAttr (Foo,'School'))#OldboyFoo.class_method ()#OldboyPrint(GetAttr (Foo,'Class_method'))#<bound method Foo.class_method of <class ' __main__. Foo ' >>GetAttr (Foo,'Class_method')()#Oldboy equivalent: Foo.class_method ()GetAttr (Foo,'Static_method')()#In Staticmethod equivalent to: Foo.static_method ()
GetAttr Instances

Python reflection, built-in functions (__str__, __repr__)

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.