stringmodule = imp.load_module('string',*imp.find_module('string'))
imp.load_source("TYACMgrHandler_"+app.upper(), filepath)
4,vars().has_key('s')
动态增加属性
class Obj(object):
pass
def main():
list=["a","b", "c"]
for i inrange(1,len(list),2):
Obj = type('Obj',(),{list[i]:lambdaself,s:obj.__setattr__(s.split(" = ")[0],s.split(" = ")[1])})
obj =Obj()
for i inrange(0,len(list),2):
obj.__setattr__(list[i],list[i])
obj.a =1
obj.b("a =2")
obj.b("c =3")
printobj.a
printobj.c
if __name__ == '__main__':
main()
动态载入包
根据名字加载指定文件
def loadapp(self, app):
filepath="mgr/"+app+".py"
if os.path.exists(filepath):
imp.load_source("TYACMgrHandler_"+app.upper(), filepath)
//修改了app.py,从新调用这个函数,新的代码自动生效
根据名字调用对应方法
return getattr(self, op)(args.get("port"), args) //op="start" args=dict
getattr(self, self.request.method.lower())(*args, **kwargs)