Table of Contents
variable with underscore
- Python Docs - Private Variables and Class-local References
- The meaning of a single- and a double-underscore before an object name in Python
- Underscore vs Double underscore with variables and methods
- Private, protected and public in Python
@nmichaels's answer:
- _single_leading_underscore: weak "internal use" indicator. E.g. "from M import *" does not import objects whose name starts with an underscore. - single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g. Tkinter.Toplevel(master, class_='ClassName') - __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below). - __double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.
property method
- Python Docs - property
- Python @property versus getters and setters
- Python进阶之“属性(property)”详解
- Properties vs. Getters and Setters
计算目录的md5
也就是遍历目录, 计算所有文件内容合并后的md5值。
注意: 保证遍历目录的路径是一致的, 否则跨平台遍历方式不一致导致md5不同。
见Simiki中的实现: Add directory md5 hash function, 另外, 这个可能还需要考虑内存溢出的情况? TODO
另外可参考:
- How do I get the MD5 sum of a directory's contents as one sum?
- Getting the SHA-1 (or MD5) hash of a directory (Python recipe)
pip install from git repo
$ pip install -e git+https://github.com/username/projname.git#egg=projname
参考:
virtualenv对系统级包的策略
最近发现使用virtualenv进入一个隔离环境时, 每次cd切换目录都会报错, 原因是autojump(py写的)模块找不到。
排查后发现是系统级的python包目录不在sys.path
中。
看了virtualenv的changelog发现这个是在1.7版本做的变更:
- virtualenv-1.7 之前, 默认策略是把把系统包路径也加入
sys.path
. 有--no-site-packages
在创建虚拟环境时, 可以不加入系统包路径. - virtualenv-1.7和之后, 默认策略是上面的
--no-site-packages
. 并且增加了新选项--system-site-packages
, 即上面情况的默认策略.
具体可以看看virtualenv 的 changelog
我之前应该很长一阵子都处于1.7之前的版本, 最近才作了下升级.
list和dict释放空间
list:
a = []
新赋值, 其它引用不变del a
删除a, 其它引用不变del a[:]
清空a的元素, 所有引用和a保持一样变化, 也为空
dict:
- Difference between dict.clear() and assigning {} in Python
- Does dictionary's clear() method delete all the item related objects from memory?
d = {}
新赋值,其它引用不变del d
删除d, 其它引用不变d.clear()
清空d的元素, 所有引用和d保持一样变化, 也为空