python code

时间:2022-09-14 05:00:26

执行动态语句

执行字符串中的代码

http://www.cnblogs.com/fanweibin/p/5418817.html

#!usr/bin/env python
#coding:utf-8 namespace = {'name':'xs','data':[18,73,84]} code = '''def hellocute():return "name %s ,age %d" %(name,data[0],) ''' func = compile(code, '<string>', "exec") exec func in namespace result = namespace['hellocute']() print result

动态加载模块

根据配置信息加载模块,

import config as conf
from importlib import import_module as im import tornado.web as web
import os urlpatterns = [] for app in conf.INSTALLED_APPS:
cmd = app + '.urls'
urlpatterns = urlpatterns + getattr(im(cmd), 'urlpatterns')