Python 中的那些坑总结——持续更新

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()

原文地址:https://www.cnblogs.com/wangchaowei/p/9002734.html

时间: 2024-08-01 08:22:35

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

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

初学python,踩了许多坑...每天都学一点吧..(大佬绕过) 1.session的用法: session是python requests库中的一个重要功能.session可以存储用户的数据并且存储在服务器端,相当于用户的一个唯一凭证. cookie也会存在在session中. 如果说我们想要用session进行保持会话的请求以及后续的post,get等操作,以及返回的话.需要先用一边session. 我的理解是session刚开始是空的,先进行一次get的操作,然后保存了我们的用户信息.之后

Angular js开发的各种坑(持续更新中。。。)

Angular UI的Modal 在按照官方文档写的时候会报如下错误 var ModalDemoCtrl = function ($scope, $modal, $log) {   $scope.items = ['item1', 'item2', 'item3'];   $scope.open = function (size) {    var modalInstance = $modal.open({       templateUrl: 'myModalContent.html',   

使用JEECG过程中的问题汇总(持续更新)

1.首次启动Tomcat服务时,控制台信息提示请使用SQL Server 2005或更高版本. <dependency> <groupId>org.jeecgframework</groupId> <artifactId>jdbc2005</artifactId> <version>${sqlserver.version}</version> <scope>runtime</scope> </

python使用小技巧汇总(持续更新)

博客园开了接近四年,没有写过一篇文章.实在惭愧.作为一个非常不专业的IT行业爱好者,第一篇文章从随便记点流水账开始吧. 1.实现一个简易server 通过这个server,可以方便地在局域网内分享信息.文件,包括向移动端分享文件等. # Python2 python -m SimpleHTTPServer # Python 3 python3 -m http.server ps:可以通过直接添加端口号信息,指定端口访问. 2.查看api文档 python编程时,方便随时查看modules文档.

elasticsearch+kibana使用中踩的坑,持续更新中。

存进elasticsearch中的数据在kibana的discover中看不到,但是通过_id在Dev Tools可以查到,也可以直接用python取出来.最后发现存进去的时候没加时间戳. doc = { "file_name": file_name, "content": pdf_str, "timestamp": datetime.datetime.now()#没加的时间戳 } 最后找了一天...

python各种错误提示集锦,持续更新中~

一.今天遇到的一个错误 IndentationError: expected an indented block 翻译:缩进错误.检查下提示所在行,缩进格数 原文地址:https://www.cnblogs.com/taotao1904/p/12069061.html

Elasticsearch之es学习工作中遇到的坑(陆续更新)

1:es集群脑裂问题(不要用外网ip,节点角色不要混用) 原因1:阿里云服务器,外网有时候不稳定. 解决方案:单独采购服务器,内网安装 原因2:master和node节点没有分开 解决方案: 分角色:master节点(三台),data节点(随着数据增加而增加),client(随着查询压力而增加)节点 Master节点:node.master: true   node.data: false Data节点:node.master: false   node.data: true Client 节点

H5填坑笔记--持续更新

最近一直在做移动端的页面,发现很多的坑,这里做一下总结,填填坑…… css常见的问题(一) 一.iOS键盘首字母自动大写 IOS的机子,默认英文输入法状态下,首字母是自动大写的,有时候挺烦人的. 在iOS中,默认情况下键盘是开启首字母大写的功能的,如果业务不想出现首字母大写,可以这样: <input type="text" autocapitalize="off" /> 二.iOS输入框默认内阴影和样式问题 在iOS上,输入框默认有内部阴影,但无法使用

Python——各类库的安装(持续更新)

一.BeautifulSoup 说明:www.crummy.com:Beautiful Soup 3只能在python2.x版本中运行,而Beautiful Soup 4还可以在python3.x版本中运行.Beautiful Soup 4速度更快,特性更多,而且与第三方的文档解析库(如lxml和html5lib)协同工作. 方法1.在cmd窗口下,输入: > pip install beautifulsoup4 即可完成下载. 方法2.https://www.crummy.com/softwa