python的一些笔记


1.用select来实现输入超时的方法
  1. def input(timeout):
  2.     rd = select.select([sys.stdin],[],[],timeout)[0]
  3.     if not rd:
  4.         return None
  5.     else:
  6.         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指南的时候,看到有这么一段文字“小漏洞:派生类和基类取相同的名字就可以使用基类的私有变量”。这是什么漏洞?我写了简单的示例来说明这个“漏洞”:
  1. class Test:
  2.     __str = 'hello world'
  3. class Test(Test):
  4.     def p(self)
  5.         print self.__str

测试:
t = Test()
t.p()

结果:
hello world

可以看出t是派生类Test的实例,而不是基类Test的实例,在派生类Test中可以引用到基类Test的私有变量__str。按定义来说类的私有变量只能为自己使用,而上面的方法却可达到使用基类的私有变量,这就是所谓的“漏洞”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值