1.用select来实现输入超时的方法
- def input(timeout):
- rd = select.select([sys.stdin],[],[],timeout)[0]
- if not rd:
- return None
- else:
- return raw_input()
用例:
input(5) #五秒没输入,就超时返回
2.用urllib.open进行HTTP访问时,带参数是POST模式,不带参数是GET模式
3.os.walk函数的第二个参数topdown为False,即为先遍历文件,在遍历文件夹,从深层文件夹遍历出
文档中的注释:
When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune
the search, or to impose a specific order of visiting. Modifying
dirnames when topdown is false is ineffective, since the directories in
dirnames have already been generated by the time dirnames itself is
generated.
4. 最近在看python指南的时候,看到有这么一段文字“小漏洞:派生类和基类取相同的名字就可以使用基类的私有变量”。这是什么漏洞?我写了简单的示例来说明这个“漏洞”:
- class Test:
- __str = 'hello world'
- class Test(Test):
- def p(self)
- print self.__str
测试:
t = Test()
t.p()
结果:
hello world
可以看出t是派生类Test的实例,而不是基类Test的实例,在派生类Test中可以引用到基类Test的私有变量__str。按定义来说类的私有变量只能为自己使用,而上面的方法却可达到使用基类的私有变量,这就是所谓的“漏洞”。