install python module
摘要:【install python module】 参考:http://docs.python.org/2.7/install/index.html
阅读全文
python's descriptor II
摘要:【python's descriptor II】 For instance,a.xhas a lookup chain starting witha.__dict__['x'], thentype(a).__dict__['x'], and continuing through the base ...
阅读全文
python's fnmatch&glob&os.listdir
摘要:[python's fnmatch&glob&os.listdir]fnmatch: fnmatch只有4种special character,用于提供和shell中一样的文件名的匹配. For a literal match, wrap the meta-characters in brackets. For example,'[?]'matches the character'?'. 主要函数有: fnmatch.fnmatch(), fnmatch.fnmatchcase(), fnmatch.filter(), fnmatch.t
阅读全文
python's unittest
摘要:【python's unittest】 unittestsupports some important concepts: 从上图可以看到,unittest中的test-case、test-suit概念和一般的unittest中的内容没有区别。 下面给出编写单元测试的示例: 编写要点: 1、继承自unittest.TestCase。 2、setUp、tearDown就是所谓的fixture。 3、测试函数以test开头。 开启测试: TheTestCaseclass provides a number of methods to check f...
阅读全文
python 文件的读取&更新
摘要:[python 文件的读取&更新] 任务抽象: 读取一个文件, 更新内容后, 重新写入文件. 实际应用: 磁盘上的一个配置文件, 读入内存后为一个dict, 对dict更新后重新写入磁盘. demo: 关键应用在于读取文件后,使用seek&truncate把文件给清空, 然后再写入.参考:http://docs.python.org/2.7/library/stdtypes.html#file-objects
阅读全文
python's metaclass
摘要:【python's metaclass】 和objc中类似,metaclass用于创建一个类对象,但与objc不同的是,objc中每个类对象有各自不同的metaclass,而python中的metaclass主要用于创建class object。 首先,type可以像这样工作: __metaclass__属性 那么在__metaclass__中放置些什么代码呢? This allows classes or functions to be written which monitor or alter the class creation process.参考: 1...
阅读全文
python's descriptor
摘要:【python's descriptor】1、实现了以下三个方法任意一个的,且作为成员变量存在的对象,就是descriptor。 1)object.__get__(self,instance,owner):instance是实例的引用,owner是类对象的引用。 2)object.__set__...
阅读全文
__str__&__repr__
摘要:【__str__&__repr__】 object.__str__(self): Called by the str() built-in function and by the print statement to compute the “informal” string representat
阅读全文
python要点之III
摘要:【python要点之III】1、实现交换。 在C/C++中,交换两个变量,需要2个变量,tmp=x;x=y;y=tmp;。 在python中,交换两个变量可以这么写:x,y=y,x。2、is&is not操作符用于测试两个变量是否指向同一个对象。 a is b 等价于 id(a)==id(b)。3、//是地板除运算符。4、[::x]是步长切片,例如:s='abcdefgh',则s[::2]的结果是'aceg'。5、__dict__内部cpython实现使用了hash_table,非常耗内存,为了节省内存,可以在定义属性时使用__slots__,__slo
阅读全文
python's is&==区别
摘要:【python's is&==区别】 通常我们写: 1 if foo is None: pass 这个写法与以下的写法有何区别呢? 1 if foo == None: pass is当比较的是相同的对象实例时总是返回True。而==则完全决定于__eq__()方法的实现。例如:1 >>> class foo(object):2 def __eq__(self, other):3 return True4 5 >>> f = foo()6 >>> f == None7 True8 >>> f is None9
阅读全文
python's decorator&wrapper
摘要:[python's decorator&wrapper] decorator A function returning another function, usually applied as a function transformation using the @wrapper syntax.
阅读全文
objects & values & types
摘要:[objects & values & types] 1、Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may th
阅读全文
partial function
摘要:[partial function] functools.partial(func[,*args][, **keywords]) Return a newpartialobject which when called will behave likefunccalled with the positional argumentsargsand keyword argumentskeywords. If more arguments are supplied to the call, they are appended toargs. If additional keyword argume..
阅读全文
python传值&值引用
摘要:[python传值&值引用] 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值--相当于通过“传引用”来传递对象。如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象--相当于通过“传值'来传递对象。 python一般内部赋值变量的话,都是传个引用变量,和C语言的传地址的概念差不多。可以用id()来查询内存地址.如果a=b的话, a和b的
阅读全文
Unpacking Argument Lists
摘要:[Unpacking Argument Lists] The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-inrange()function expects separatestartandstoparguments. If they are not available s.
阅读全文
python's try&except&else
摘要:【python's try&except&else】 python的try&catch有个好用的东西,else,即try&except&else可以共用,else用于当无异常时执行。socket.getfqdn就是一个教科书般的例子。 except元组:对于 except ValueError, e,这并不会产生一个元组,而是ValueError as e。【自定义异常】 exceptinos是python内置的一个module,一些常用的标准异常也被内置,其中BaseException是所有内置异常的基类,Exception也继承于BaseEx
阅读全文
python's output redirect
摘要:[python's output redirect]fin = open("xx.txt", 'r');print >>fin, "hello world", 12;fin.close();
阅读全文
python's os.system&os.spawn
摘要:[python's os.system&os.spawn] os.system会新建一个子shell,在子shell中执行传入的sh脚本。os.spawn用于执行一个bin,产生一个子进程。所以这2个东西表面看来是不一样的。 不过通过精心构造的sh脚本,也可以通过os.system实现os.spawn的功能,但是麻烦。 参考:http://docs.python.org/2.7/library/os.html
阅读全文
python's default parameter
摘要:[python's default parameter] 对于值类型(int、double)的default函数参数,函数不会保存对默认类型的修改。对于mutable objectd类型的默认参数,会有累积效应。 参考:http://docs.python.org/2.7/tutorial/controlflow.html
阅读全文
python's mutable & immutable
摘要:【python's mutable & immutable】 python里面的类型其实也分为immutable和mutable二种,对于mutable,如果b指向a,当b改变时,a也会改变;对于immutable,如果b改变,不会影响a。 参考:http://www.ibaiyang.org/2012/05/04/howtopython/
阅读全文