标签:
(1).对是否是模块,框架,函数等进行类型检查。
(2).获取源码
(3).获取类或函数的参数的信息
(4).解析堆栈
7、python标准库:http://python.usyiyi.cn/python_278/library/index.html
L[ : ] = [min(x, 100) for x in L] 此时的L并没有重新绑定一个新的列表,而是修改了原来列表的内容。
multilist = [[0 for col in range(5)] for row in range(3)] multilist2 = [[0] * 5] * 3 虽然上面这个很简洁,不过会出现共享引用问题,即multilist2[0] == multilist2[1]
24、 循环import模块会怎样?
python中循环导入不会怎么样,因为每个模块被import的时候只会执行一次,并且该模块的引用会存放在sys.modules中,后面如果再import该模块时,虚拟机会查看sys.modules是否存在该模块,如果存在则不导入。看看下面一个例子就一目了然了:
test.py:
1 import sys 2 print ‘test module‘ 3 print ‘before import test2‘, sys.modules.keys() 4 import test2 5 print ‘after import test2‘, sys.modules.keys() 6 if __name__ == ‘main‘: 7 import test
test2.py
1 import sys 2 print ‘test2 module‘ 3 print ‘before import test‘, sys.modules.keys() 4 import test 5 print ‘after import test‘, sys.modules.keys()
运行结果:
1 test module 2 before import test2 [‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 3 test2 module 4 before import test [‘test2‘, ‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 5 test module 6 before import test2 [‘test2‘, ‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘test‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 7 after import test2 [‘test2‘, ‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘test‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 8 after import test [‘test2‘, ‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘test‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 9 after import test2 [‘test2‘, ‘copy_reg‘, ‘sre_compile‘, ‘locale‘, ‘_sre‘, ‘functools‘, ‘encodings‘, ‘site‘, ‘__builtin__‘, ‘sysconfig‘, ‘operator‘, ‘__main__‘, ‘types‘, ‘encodings.encodings‘, ‘encodings.gbk‘, ‘abc‘, ‘_weakrefset‘, ‘encodings._codecs_cn‘, ‘errno‘, ‘encodings.codecs‘, ‘sre_constants‘, ‘re‘, ‘_abcoll‘, ‘ntpath‘, ‘_codecs‘, ‘test‘, ‘encodings._multibytecodec‘, ‘nt‘, ‘_warnings‘, ‘genericpath‘, ‘stat‘, ‘zipimport‘, ‘encodings.__builtin__‘, ‘warnings‘, ‘UserDict‘, ‘_multibytecodec‘, ‘sys‘, ‘codecs‘, ‘os.path‘, ‘_functools‘, ‘_codecs_cn‘, ‘_locale‘, ‘signal‘, ‘traceback‘, ‘linecache‘, ‘encodings.aliases‘, ‘exceptions‘, ‘sre_parse‘, ‘os‘, ‘_weakref‘] 10 [Finished in 0.2s]
从执行结果来看,test先import test2,由于sys.modules中没有test2,所以执行test2,并将test2加入sys.modules中;在test2中,import test1,由于sys.modules中没有test1,所以执行test1,并将test1加入sys.modules中;执行到import test2时,由于此时sys.modules中存在了test2,所以不执行test2,等到test1执行完成后回到test2继续执行;test2继续执行完成后回到最先的test执行。
25、pickle对象持久化
pickle用法很简单,将一个python对象通过dumps序列化为字符串,如果通过loads将一个str转化为一个python对象。具体例子如下:
1 >>> t1 = (‘this string‘, 42, [1, 2, 3]) 2 >>> import pickle 3 >>> p1 = pickle.dumps(t1) 4 >>> p1 5 "(S‘this string‘\np0\nI42\n(lp1\nI1\naI2\naI3\natp2\n." 6 >>> t2 = pickle.loads(p1) 7 >>> t2 8 (‘this string‘, 42, [1, 2, 3]) 9 >>>
26、 自定义迭代器
在class中定义__iter__和next函数即可,具体如下:
1 class Iter(object): 2 def __init__(self, owner, start, stop): 3 self.owner = owner 4 self.value = start - 1 5 self.stop = stop 6 def next(self): 7 if self.value == self.stop: 8 raise StopIteration 9 self.value += 1 10 return self.value ** 2 11 12 class Squares(object): 13 14 def __init__(self, start, stop): 15 self.start = start 16 self.stop = stop 17 def __iter__(self): 18 return Iter(self, self.start, self.stop) 19 20 x = Squares(1, 5) 21 for i in x: 22 for j in x: 23 print i, ‘ ‘, j
27、内置函数locals和globals
主要是语句执行的上下文环境。
标签:
原文地址:http://www.cnblogs.com/chengxuyuancc/p/4856342.html