python笔记(持续更新)
(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:
import sys
print 'test module'
print 'before import test2', sys.modules.keys()
import test2
print 'after import test2', sys.modules.keys()
if __name__ == 'main':
import test
test2.py
import sys
print 'test2 module'
print 'before import test', sys.modules.keys()
import test
print 'after import test', sys.modules.keys()
运行结果:
test module
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']
test2 module
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']
test module
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']
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']
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']
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']
[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对象。具体例子如下:
>>> t1 = ('this string', 42, [1, 2, 3])
>>> import pickle
>>> p1 = pickle.dumps(t1)
>>> p1
"(S'this string'\np0\nI42\n(lp1\nI1\naI2\naI3\natp2\n."
>>> t2 = pickle.loads(p1)
>>> t2
('this string', 42, [1, 2, 3])
>>>
26、 自定义迭代器
在class中定义__iter__和next函数即可,具体如下:
class Iter(object):
def __init__(self, owner, start, stop):
self.owner = owner
self.value = start -
self.stop = stop
def next(self):
if self.value == self.stop:
raise StopIteration
self.value +=
return self.value ** class Squares(object): def __init__(self, start, stop):
self.start = start
self.stop = stop
def __iter__(self):
return Iter(self, self.start, self.stop) x = Squares(, )
for i in x:
for j in x:
print i, ' ', j
27、内置函数locals和globals
主要是语句执行的上下文环境。
python笔记(持续更新)的更多相关文章
- BLE资料应用笔记 -- 持续更新
BLE资料应用笔记 -- 持续更新 BLE 应用笔记 小书匠 简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.'让我们'更深入地探索这些方面吧. 蓝牙无处不在-,您可以在几乎每一台电话.笔记本电 ...
- Python奇技淫巧 - 持续更新中....
Python奇技淫巧 人生苦短,我用Python: 编程界这绝对不是一句空话,尤其是对于使用过多个语言进行工作的同学们来说,用Python的时间越长,越有一种我早干嘛去了的想法,没事,啥时候用Pyth ...
- [读书]10g/11g编程艺术深入体现结构学习笔记(持续更新...)
持续更新...) 第8章 1.在过程性循环中提交更新容易产生ora-01555:snapshot too old错误.P257 (这种情况我觉得应该是在高并发的情况下才会产生) 假设的一个场景是系统一 ...
- react-native-storage 使用笔记 持续更新
React-native-storage是在AsyncStorage之上封装的一个缓存操作插件库,刚开始接触这个也遇到了一些问题,在这里简单记录总结一下,碰到了就记下来,持续更新吧 1.安卓下stor ...
- python内置模块笔记(持续更新)
常用函数name = '{wh}my \t name is {name},age is {age}.' print(name.capitalize()) # 字符串的开头字母大写 print(name ...
- 数据分析之Pandas和Numpy学习笔记(持续更新)<1>
pandas and numpy notebook 最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...
- BLE资料应用笔记 -- 持续更新(转载)
简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.’让我们’更深入地探索这些方面吧. 蓝牙无处不在—,您可以在几乎每一台电话.笔记本电脑 .台式电脑和平板电脑中找到蓝牙.因此,您可以便利地连接键盘 ...
- Semantic ui 学习笔记 持续更新
这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...
- Git学习笔记(持续更新)
1.强制同步为远程的代码 远程仓库回退了commit的情况下(第2条描述之情况),强制同步远程的代码到本地 #更新远程最新的所有代码,但是不merge或者rebase git fetch --all ...
- web前端开发随手笔记 - 持续更新
本文仅为个人常用代码整理,供自己日常查阅 html 浏览器内核 <!--[if IE]><![endif]--> <!--[if IE 6]><![endif ...
随机推荐
- Database Replay和Consolidated Database replay
简介 在数据库的迁移和升级场景中,我们经常会遇到一个问题:在做压力测试时,如何模拟真实的业务压力,解决这个问题的方法有很多,比如:应用方开发模拟程序或者使用压力测试工具模拟,如load runner, ...
- 关于面试题 Array.indexof() 方法的实现及思考
这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...
- 开始学nodejs——net模块
net模块的组成部分 详见 http://nodejs.cn/api/net.html 下面整理出了整个net模块的知识结构,和各个事件.方法.属性的用法 net.Server类 net.Socket ...
- [.NET] 利用 async & await 的异步编程
利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html 目录 异步编程的简介 异 ...
- jQuery.template.js 简单使用
之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...
- scp报错 -bash: scp: command not found
环境:RHEL6.5 使用scp命令报错: [root@oradb23 media]# scp /etc/hosts oradb24:/etc/ -bash: scp: command not fou ...
- C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- Java中用得比较顺手的事件监听
第一次听说监听是三年前,做一个webGIS的项目,当时对Listener的印象就是个"监视器",监视着界面的一举一动,一有动静就触发对应的响应. 一.概述 通过对界面的某一或某些操 ...
- CSS入门常见的问题
写在前面:本文简单介绍一下css的三大特性:层叠性.继承性.优先级.以及margin,padding,浮动,定位几个知识点.限于水平,不深入探讨,仅作为学习总结. 1,三特性 1)层叠性:同标签同权重 ...
- js闭包for循环总是只执行最后一个值得解决方法
<style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;curso ...