python code

python

执行动态语句

执行字符串中的代码
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')

以上是 python code 的全部内容, 来源链接: utcz.com/z/388298.html

回到顶部