1. Reflection mechanism
#/usr/bin/env python#-*-Coding:utf-8 The core essence of the Python reflection mechanism: to manipulate (Find/Get/delete/Add) members in the form of a string into an object (module), A kind of string-based event-driven popular talk is the user through the different URL, call different module functions, many of the more popular web frameworks are through the mechanism of reflection, according to the different URL points to different modules GetAttr (), hasattr (), SetAttr (), Delattr () changes to the module are carried out in memory and do not affect the actual contents of the file "Def run_reflect (): receive the parameter as a #以 module/function getstr = Raw_input (' Enter the execution path: ') #把模 Block and function Split modulestr,funstr = Getstr.split ('/') Try: #以字符串的形式导入模块 module = __import__ (MODULESTR) #hasattr () built-in function to determine if a property or method exists in the module object if Hasattr (MODULE,FUNSTR): #getattr () gets the properties or methods of the module object func = Get attr (module, FUNSTR) #执行方法 func () else:print ' module < ' + Modulestr + ' > does not exist ' + Funstr + ' properties or methods! ' #setattr (object, string, arbitrary value) sets the object property value SetAttr (module, ' Setattrvalue ', 123) #判断刚设置的属性是否存在, exists as true, otherwise false Print hasattr (module, ' Setattrvalue ') #打印结果: True #以字符串的形式删除对象的属性 delattr (module, ' Setattrvalue ') Print hasattr (module, ' SetattrvalUE ') #打印结果: False except Importerror,e:print ' Module object < ' + Modulestr + ' > not present && e: ' + EIF __na me__ = = ' __main__ ': Run_reflect ()
-REFLECT_PATH1/FOO1 module/function for reference testing
#/usr/bin/env python#-*-coding:utf-8-*-from pprint import pprint #打印的增强版class test1 (): commonattr = ' Binguo ' D EF Innerfun (self): print ' Innerfun ' def foo1 (): dict = {' uid ': ' 20170806 ', ' info ': ' Reflect Test '} pprint ( Dict
2. Decorative Device
#/usr/bin/env python#-*-coding:utf-8-*-"adorner: Used to encapsulate the code of a function or class because the function exists as a primary object in Python, they can be passed to another function just like any other object. An adorner is a function that takes another function as an argument and uses it to perform some operation. An adorner's application is implemented by placing an @ character before the name of the adorner and adding a line above the decorated function declaration. One thing to note: For a callable function, you can use multiple adorners, and multiple adorners need to apply them in the bottom-up order. #内置的装饰器有三个, respectively, are @staticmethod, @classmethod, and @property, respectively, to change the instance methods defined in the class into static methods, class methods, and Class properties. See below: "' #无参情况def Decorator1 (fun): Def innerdef (): print ' before decorator ' #函数运行前处理常见的前置条件 (such as authorization) fun () #实 The parameterless function of the international decorator to encapsulate print ' after decorator ' #函数处理后确保清理 (such as output exception handling) return Innerdef #有参情况def Decorator2 (fun): #装饰器可将函数注册到 Signaling system or registering to the Web application's URL registry, in short, useful, later to Def innerdef (args): print ' Args:before decorator ' fun (args) #实际装饰器要封装 The parameter print ' Args:after decorator ' return innerdef @decorator1 # parameterless function application Adorner def foo2 (): print ' function foo 2 ' @decorator2 # parametric function application Adorner def FOO3 (argsvalue): print ' function Foo3 ' + ' \t\t ' + ' argsvalue: ' + argsvalue foo 2 () Foo3 (' when input args ')
Python learning Experience (vi) reflection mechanism, adorners