1.三元表达式之坑

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

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

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

from os import path

def walk(top, topdown=True, onerror=None, followlinks=False):
islink, join, isdir = path.islink, path.join, path.isdir
try:
# Note that listdir and error are globals in this module due
# to earlier import-*.
names = listdir(top)
except error, err:
if onerror is not None:
onerror(err)
return dirs, nondirs = [], []
for name in names:
if isdir(join(top, name)):
dirs.append(name)
else:
nondirs.append(name) if topdown:
yield top, dirs, nondirs
for name in dirs:
new_path = join(top, name)
if followlinks or not islink(new_path):
for x in walk(new_path, topdown, onerror, followlinks): # 生成器递归
yield x
if not topdown:
yield top, dirs, nondirs

3.try...finally + return:

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

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

  

  附加Queue的get方法:

# Queue.py
......
def get(self, block=True, timeout=None):
self.not_empty.acquire()
try:
if not block:
if not self._qsize():
raise Empty
elif timeout is None:
while not self._qsize():
self.not_empty.wait()
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
endtime = _time() + timeout
while not self._qsize():
remaining = endtime - _time()
if remaining <= 0.0:
raise Empty
self.not_empty.wait(remaining)
item = self._get()
self.not_full.notify()
return item
finally:
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. Tomcat 500错误 问题集锦

    HTTP 500 - 内部服务器错误  1.jdk版本与Tomcat版本不一样. 问题: 配置一个Web应用的时候,源文件和server.xml.web.xml的配置都没有问题,但是在访问到一个Ser ...

  2. python threading.thread

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  3. Tomcat 系统架构

    https://www.ibm.com/developerworks/cn/java/j-lo-tomcat1/index.html 2010 年 5 月 20 日发布 本文以 Tomcat 5 为基 ...

  4. 一道题浅析 i++,++i,i+1及(引用)&i的区别

    我们可能很清楚i++,++i和i+1级&i的概念,但在实际运用中我们就有可能很容易搞混淆.特别是在递归中区别它们就显得尤为重要了.那首先我们先看一段利用递归逆序字符串的代码,你能回答出这段代码 ...

  5. 使用dockerfile 创建ubuntu ssh镜像

    ############################################################ # Dockerfile to build ubunto ssh contai ...

  6. Office 2003 2007 2010 配置进度 正在配置 解决方案 (转载)

    在安装过Office2003.2007 或者2010之后,如果没有选择全部的组件,或者是因为安装到非系统盘,有时候打开 Office 文档的时候就会出现正在配置Office,或者Office配置进度的 ...

  7. sysbench 0.4.12安装

    前提:mysql已安装完成,请参考http://www.cnblogs.com/lizhi221/p/6813907.html   安装依赖环境包: yum install -y bzr yum in ...

  8. 数据挖掘-KNN-K最近邻算法

    1. 算法核心思想: 通过计算每个训练样本到待分类样本的距离,选取和待分类样本的距离最近的 K 个训练样本,K个样本中那个类别的训练样本占据着多数, 则表明待分类的样本就属于哪一个类别. KNN算法在 ...

  9. gcc安装多个版本

    http://blog.csdn.net/chid/article/details/6251781

  10. MySQL基础语句【学习笔记】

    放在这里,以备后查. 1. 数据库, 数据库服务器, 数据库语言 数据库,是持久性数据的集合,供给定企业的应用程序系统使用,并且由一个数据库管理系统来管理: 数据库服务器,又称数据库管理系统,用来管理 ...