About the reflection of Python, the practice of decorating

Source: Internet
Author: User

From the basic concept, a simple example can slowly go to the high point of the place.

In addition, the functional programming of Python is also a point of interest to me.

Overall, I think that OOP can be a big frame and idea, FP can make details when the elegant ox x.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Introspection" should be the original concept, in particular, the ability to obtain the object's own information at run time,
"Reflection" is an embodiment of introspection, is concrete.
Introspection is "Tao" and Reflection is "technique".

A "variable scope" is a concept, and "closure" is a specific technique in scope.

A language can have complete introspection without "reflection", or it can have a full scope function without a "closure"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Import TimedefTimeit (func):defwrapper (): Start=Time.clock () func () End=Time.clock ()Print 'used:', End-StartreturnWrapper@timeit#foo = Timeit (foo)deffoo ():Print 'In foo ()'    PrintFoo ()
#Coding:utf-8ImportSys#module, sys points to this module objectImportInspectdefFoo ():Pass #function, Foo points to this function object classCat (object):#class, cat points to this class object    def __init__(Self, name='Kitty'): Self.name=namedefSayhi (self):#instance method, Sayhi points to the method object, using the class or instance. Sayhi Access        PrintSelf.name,'says hi!' #access the field named name, using the instance. Name AccessCat= Cat ()#Cat is an instance object of cat class PrintCat.sayhi#when accessing an instance method using the class name, the method is unbound (unbound)PrintCat.sayhi#when accessing an instance method using an instance, the method is bound (bound)Cat= Cat ('Kitty') PrintCat.name#Accessing Instance PropertiesCat.sayhi ()#Invoking instance methods PrintDir (cat)#gets the property name of the instance, returned as a listifHasattr (CAT,'name'):#Check if the instance has this propertySetAttr (CAT,'name','Tiger')#same as:a.name = ' Tiger 'PrintGetAttr (CAT,'name')#same As:print A.nameGetAttr (CAT,'Sayhi')()#same as:cat.sayHi ()PrintCat.__doc__           #NonePrintCat.__name__          #CatPrintCat.__module__        #__main__PrintCat.__bases__         #(<TYPE>,)PrintCat.__dict__          #{' __module__ ': ' __main__ ', ...} </type>PrintCat.__dict__PrintCat.__class__PrintCat.__class__= = Cat#Trueim=Cat.sayhiPrintIm.im_funcPrintIm.im_self#CatPrintIm.im_class#CatCo=Cat.sayHi.func_codePrintCo.co_argcount#1PrintCo.co_names#(' name ',)PrintCo.co_varnames#(' self ',)PrintCo.co_flags & 0b100#0im=Cat.sayhiifinspect.isroutine (IM): Im ()PrintInspect.getmro (Cat)#(<class ' __main__. Cat ';, <type ' object ' >)PrintCat.__mro__#(<class ' __main__. Cat ';, <type ' object ' >)classDog:PassPrintInspect.getmro (Dog)#(<class __main__. Dog at 0x...>,)PrintDog.__mro__ #Attributeerror

About the reflection of Python, the practice of decorating

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.