c = {}
def foo():
f = dict(zip(list("abcd"), [1, 2 ,3 ,4]))
c.update(f)
if __name__ == "__main__":
a = b = d = c
b['e'] = 5
d['f'] = 6
foo()
print(a)
print(b)
print(c)
print(d)
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{}
{'e': 5}
{}
{'f': 6}
>>>a = {1:1, 2:2}
>>>b = a
>>>a[3] = 3
>>>b
{1: 1, 2: 2, 3: 3}
>>>a = 4
>>>b
{1: 1, 2: 2, 3: 3}
>>>a
4
class TestObj(object):
pass
x = TestObj()
x.x = 8
d = {"a": 1, "b": 2, "g": x}
xx = d.get("g", None)
xx.x = 10
print("x.x:%s" % x.x)
print("xx.x: %s" % xx.x)
print("d['g'].x: %s" % d['g'].x)
# Out:
# x.x:10
# xx.x: 10
# d['g'].x: 10
class TestObj(object):
pass
x = TestObj()
x.x = 8
d = {"a": 1, "b": 2, "g": x}
print(d['g'].x)
xx = d["g"]
xx.x = 10
print(d['g'].x)
xx = 20
print(d['g'].x)
# Out:
# 8
# 10
# 10
class Context(object): pass def foo(context): context.a = 10 context.b = 20 x = 1 def hoo(context): context.c = 30 context.d = 40 x = 1 if __name__ == "__main__": context = Context() x = None foo(context) hoo(context) print(x) print(context.a) print(context.b) print(context.c) print(context.d) # Out: # None # 10 # 20 # 30 # 40
from __future__ import print_function
def initialize(context):
g.a = 333
g.b = 666
context.x = 888
def handle_data(context, data):
g.c = g.a + g.b + context.x + context.y
a = np.array([1, 2, 3, 4, 5, 6])
print("outer space: a is %s" % a)
print("outer space: context is %s" % context)
from __future__ import print_function
import sys
import imp
from pprint import pprint
class Context(object):
pass
class PersistentState(object):
pass
# Script starts from here
if __name__ == "__main__":
outer_code_moudle = imp.new_module('outer_code')
outer_code_moudle.__file__ = 'outer_code.py'
sys.modules["outer_code"] = outer_code_moudle
outer_code_scope = code_scope = outer_code_moudle.__dict__
head_code = "import numpy as npnfrom main_exec import PersistentStateng=PersistentState()"
exec(head_code, code_scope)
origin_global_names = set(code_scope.keys())
with open("outer_code.py", "rb") as f:
outer_code = f.read()
import __future__
code_obj = compile(outer_code, "outer_code.py", "exec", flags=__future__.unicode_literals.compiler_flag)
exec(code_obj, code_scope)
# 去除掉内建名字空间的属性,仅保留外部代码中添加的属性
outer_code_global_names = set(outer_code_scope.keys()) - origin_global_names
outer_func_initialize = code_scope.get("initialize", None)
outer_func_handle_data = code_scope.get("handle_data", None)
context = Context()
context.y = 999
outer_func_initialize(context)
outer_func_handle_data(context, None)
g = outer_code_scope["g"]
assert g.c == 2886
print("g.c: %s" % g.c)
print(dir(g))
print(dir(context))
pprint(outer_code_moudle.__dict__)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有