1.三元表达式之坑

很显然,Python把第一行的(10 + 4)看成了三元表达式的前部分,这个坑是看了《Python cookbook》(P5)中学到的,书中的代码:

2.Python生成器(yield)+递归

前两天一直纠结python的生成器递归该怎么写,今天看了os.walk()的代码恍然大悟,编程真是博大精深啊!不多说,上代码:

  1. from os import path
  2.  
  3. def walk(top, topdown=True, onerror=None, followlinks=False):
  4. islink, join, isdir = path.islink, path.join, path.isdir
  5. try:
  6. # Note that listdir and error are globals in this module due
  7. # to earlier import-*.
  8. names = listdir(top)
  9. except error, err:
  10. if onerror is not None:
  11. onerror(err)
  12. return
  13.  
  14. dirs, nondirs = [], []
  15. for name in names:
  16. if isdir(join(top, name)):
  17. dirs.append(name)
  18. else:
  19. nondirs.append(name)
  20.  
  21. if topdown:
  22. yield top, dirs, nondirs
  23. for name in dirs:
  24. new_path = join(top, name)
  25. if followlinks or not islink(new_path):
  26. for x in walk(new_path, topdown, onerror, followlinks): # 生成器递归
  27. yield x
  28. if not topdown:
  29. yield top, dirs, nondirs

3.try...finally + return:

  如果写了try模块内有return且最后还有finally,那么会执行return后再执行finally。

  这是从Queue的get方法里看到的,贴上自己写的测试代码:

  

  附加Queue的get方法:

  1. # Queue.py
  2. ......
  3. def get(self, block=True, timeout=None):
  4. self.not_empty.acquire()
  5. try:
  6. if not block:
  7. if not self._qsize():
  8. raise Empty
  9. elif timeout is None:
  10. while not self._qsize():
  11. self.not_empty.wait()
  12. elif timeout < 0:
  13. raise ValueError("'timeout' must be a non-negative number")
  14. else:
  15. endtime = _time() + timeout
  16. while not self._qsize():
  17. remaining = endtime - _time()
  18. if remaining <= 0.0:
  19. raise Empty
  20. self.not_empty.wait(remaining)
  21. item = self._get()
  22. self.not_full.notify()
  23. return item
  24. finally:
  25. self.not_empty.release()

Python 中的那些坑总结——持续更新的更多相关文章

  1. python网络编程的坑(持续更新)

    初学python,踩了许多坑...每天都学一点吧..(大佬绕过) 1.session的用法: session是python requests库中的一个重要功能.session可以存储用户的数据并且存储 ...

  2. python中一些有用的函数------持续更新中

    strip() 函数 用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. str2 = " Runoob " # 去除首尾空格 print (str2.strip()) ...

  3. springboot开发过程中的小坑(持续更新)

    1. 启动的Application必须放到一个package下面,如下: package com.example.kikidemo; import org.springframework.boot.S ...

  4. python中的这些坑,早看早避免。

    python中的这些坑,早看早避免. 说一说python中遇到的坑,躲坑看这一篇就够了 传递参数时候不要使用列表 def foo(num,age=[]): age.append(num) print( ...

  5. PHP 日常开发过程中的bug集合(持续更新中。。。)

    PHP 日常开发过程中的bug集合(持续更新中...) 在日常php开发过程中,会遇到一些意想不到的bug,所以想着把这些bug记录下来,以免再犯! 1.字符串 '0.00'.'0.0'.'0'  是 ...

  6. Python:常见错误集锦(持续更新ing)

    初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...

  7. Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  8. Android开发中常用的库总结(持续更新)

    这篇文章用来收集Android开发中常用的库,都是实际使用过的.持续更新... 1.消息提示的小红点 微信,微博消息提示的小红点. 开源库地址:https://github.com/stefanjau ...

  9. Python 一些 实用的包(持续更新)

    line_profiler:(代码性能分析) 使用方法:链接 codecs:(Python内置的编码库) 数据分析与挖掘领域: 引自博客:这里     因为他有很多这个领域相关的库可以用,而且很好用, ...

随机推荐

  1. EF和PetaPoco实现快速开发

    PetaPoco是一款适用于.NET应用程序的轻型对象关系映射器(ORM, Object Relational Mapper).与那些功能完备的ORM(如NHibernate或Entity Frame ...

  2. Java类集框架——List接口

    学习目标 掌握List接口与Collection接口的关系. 掌握List接口的常用子类:ArrayList.Vector. 掌握ArrayList与Vector类的区别.    Collection ...

  3. Oracle安装部署之RAC安装环境配置脚本

    #!/bin/bash#Usage:Log on as the superuser('root'),and then execute the command:#./1preusers.sh group ...

  4. scrapy爬虫系列之一--scrapy的基本用法

    功能点:scrapy基本使用 爬取网站:传智播客老师 完整代码:https://files.cnblogs.com/files/bookwed/first.zip 主要代码: ff.py # -*- ...

  5. talib 中文文档(十二):Pattern Recognition Functions K线模式识别,形态识别

    Pattern Recognition Functions K线模式识别,形态识别 CDL2CROWS - Two Crows 函数名:CDL2CROWS 名称:Two Crows 两只乌鸦 简介:三 ...

  6. vue - vue

    一.vue - 介绍 vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp ...

  7. CSS背景以及文本

    css设置背景: <style type="text/css"> /*background-image: 直接设置x,y重复而且平铺整个body*/ /*下面两句的功能 ...

  8. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  9. Python自动发送邮件-smtplib和email库

    ''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...

  10. (转) 密码学中的“盐值 Salt”

    为什么要在密码里加点“盐” 盐(Salt) 在密码学中,是指通过在密码任意固定位置插入特定的字符串,让散列后的结果和使用原始密码的散列结果不相符,这种过程称之为“加盐”. 以上这句话是维基百科上对于 ...