内容简介:续 上文,对重复引用的各种方式作了更多测试,基于当前理解作一小结。 简言之,由于每次引用模块都对其执行(exec)一次,在多处引用同一模块的情况下,引用顺序和模块位置不同会导致不同结果。 比如在 TypeDef ...
续 上文,对重复引用的各种方式作了更多测试,基于当前理解作一小结。
简言之,由于每次引用模块都对其执行(exec)一次,在多处引用同一模块的情况下,引用顺序和模块位置不同会导致不同结果。
比如在 TypeDef 模块中定义一个类:
type Type1 { ; }
在同层目录下 Instance1 模块中声明一个个体:
using * in TypeDef
instance1 = Type1()
那么在另一模块中,先后引用 TypeDef 和 Instance1,如下的 isa 判断(对应 python 的 isinstance)返回 true,意料之中:
using * in TypeDef
using * in Instance1
print(isa(instance1, Type1))
但是以下的三种情况,全都返回 false:
- 情况 1:
using * in Instance1
using * in TypeDef
print(isa(instance1, Type1))
- 情况 2:
using TypeDef
using Instance1
print(isa(Instance1.instance1, TypeDef.Type1))
- 情况 3:
using Instance1
using TypeDef
print(isa(Instance1.instance1, TypeDef.Type1))
而如果 TypeDef 和 Instance1 在包内,行为又略有不同。如下返回 true(注意 TypeDef 在 Instance1 后引用)
using * in test.Instance1
using * in test.TypeDef
print(isa(instance1, Type1))
以下的三种情况,全都返回 false:
- 情况 4:
using * in test.TypeDef
using * in test.Instance1
print(isa(instance1, Type1))
- 情况 5:
using * in test.TypeDef
using test.Instance1
print(isa(test.Instance1.instance1, Type1))
- 情况 6:
using * in test.Instance1
using test.TypeDef
print(isa(instance1, test.TypeDef.Type1))
测试用例 集结在此。
暂没想到此种行为的应用场景。为规避,在应用中尽量保持“树式引用”,即一个模块只直接引用一次。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- es6模块 import, export 知识点小结
- Python3内置模块之json编解码方法小结
- Python3内置模块之Pickle和cPickle数据持久化方法小结
- 工厂模式的个人小结
- 面试小结之并发篇
- go 开发小结
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Making Things See
Greg Borenstein / Make / 2012-2-3 / USD 39.99
Welcome to the Vision Revolution. With Microsoft's Kinect leading the way, you can now use 3D computer vision technology to build digital 3D models of people and objects that you can manipulate with g......一起来看看 《Making Things See》 这本书的介绍吧!