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