知行编程网知行编程网  2022-05-06 03:00 知行编程网 隐藏边栏 |   抢沙发  3 
文章评分 0 次,平均分 0.0

避坑指南!Python里面的这九个坑,坑的就是你!

Python里面有一些坑,让你防不胜防,菜鸟经常会弄晕,而学习多年的Python老鸟也会时不时中招。小编整理了9个坑,都是会经常碰到的坑,让你大呼我曾经也碰到过!


虽然是小的问题,但是在实际的项目中,哪怕一个很小的错误,都会引起一连串的问题。有的时候,为了找这一个微小的bug要花费很大的力气,让人很郁闷。


坑1.lambda自由参数之坑

排序和分组的key函数常使用lambda,表达更加简洁,但是有个坑新手容易掉进去:

避坑指南!Python里面的这九个坑,坑的就是你!

但是实际却输出: 3,3,3. 定义lambda使用的i被称为自由参数,它只在调用lambda函数时,值才被真正确定下来,这就犹如下面打印出2,你肯定确信无疑吧。


避坑指南!Python里面的这九个坑,坑的就是你!


正确做法是转化自由参数为lambda函数的默认参数,就是给i一个默认的值:


避坑指南!Python里面的这九个坑,坑的就是你!


坑2. 默认参数设置为空

含有默认参数的函数,如果类型为容器,且设置为空:

避坑指南!Python里面的这九个坑,坑的就是你!

这是可变类型的默认参数之坑,请务必设置此类默认参数为None:

避坑指南!Python里面的这九个坑,坑的就是你!


坑3.共享变量未绑定之坑

有时想要多个函数共享一个全局变量,但却在某个函数内试图修改它为局部变量:

避坑指南!Python里面的这九个坑,坑的就是你!


应该在f函数内显示声明i为global变量:

避坑指南!Python里面的这九个坑,坑的就是你!


坑4.各种参数使用之坑

4.1) 位置参数必须一一对应,缺一不可

避坑指南!Python里面的这九个坑,坑的就是你!

但是下面这样的调用就不ok:

避坑指南!Python里面的这九个坑,坑的就是你!

4.2) 关键字参数必须在位置参数右边

避坑指南!Python里面的这九个坑,坑的就是你!

这样就是不行,关键字参数必须在位置参数的右边


4.3) 可变关键字参数

如下x是可变关键字参数:

避坑指南!Python里面的这九个坑,坑的就是你!

但是不能这样玩:

<span style="font-size: 15px;">f(<span style="color: rgb(174, 129, 255);">1</span>) TypeError<span style="color: rgb(249, 38, 114);">: </span>f() takes <span style="color: rgb(174, 129, 255);">0 </span>positional arguments but <span style="color: rgb(174, 129, 255);">1 </span>was given</span>


坑5  列表删除之坑

删除一个列表中的元素,此元素可能在列表中重复多次:

避坑指南!Python里面的这九个坑,坑的就是你!

考虑删除这个序列[1,5,5,5,7]中的元素5,结果发现只删除其中两个:[1, 5, 7]

原因是这个序列在删除的时候,动态的缩短,当你第二次循环的时候,已经跳过了一个5。

正确的做法,构造成字典

避坑指南!Python里面的这九个坑,坑的就是你!

利用字典的key的唯一性,这样就能很干净的删除了

坑6.相同值的不可变对象

避坑指南!Python里面的这九个坑,坑的就是你!

key=1,value=go的键值对神奇消失了,这个是什么原因,有点丈二和尚摸不着头脑。这是因为具有相同值的不可变对象在Python中始终具有相同的哈希值。由于存在哈希冲突,不同值的对象也可能具有相同的哈希值。


坑7.谜一样的字符串驻留

避坑指南!Python里面的这九个坑,坑的就是你!

如果上面例子返回True,但是下面例子我们稍微换一下,加入一个@为什么是False:

避坑指南!Python里面的这九个坑,坑的就是你!


这与Cpython 编译优化相关,行为称为字符串驻留,但驻留的字符串中只包含字母,数字或下划线。

坑8.奇怪的for

避坑指南!Python里面的这九个坑,坑的就是你!

是不是很奇怪,执行了一次for循环之后,i就变成了5,为什么不是执行一次就退出?其实for在Python中的工作方式是这样的,range(5)生成的下一个元素就被解包,并赋值给目标列表的变量i,所以 i = 5 并不会影响循环。


坑9.生成器的执行时机

避坑指南!Python里面的这九个坑,坑的就是你!

g为生成器,list(g)后返回[2,4,6],因为每个元素肯定至少都出现一次。所以这个结果这不足为奇。但是,请看下例:


避坑指南!Python里面的这九个坑,坑的就是你!

这是不是有点不可思议~~ 原因在于,生成器表达式中, in 子句在声明时执行, 而条件子句则是在运行时执行。

或者我们这样解释就明白了:

避坑指南!Python里面的这九个坑,坑的就是你!


<section><section mpa-from-tpl="t"><section><pre><p><span style="font-size: 15px;"><br mpa-from-tpl="t"  /></span></p><section data-mpa-template="t" mpa-from-tpl="t"><section style="width: 100%;display: flex;flex-direction: row;justify-content: center;" data-mid="" mpa-from-tpl="t"><section data-mid="" mpa-from-tpl="t" style="width: 182px;height: 19px;background-image: url('https://www.zkxjob.com/wp-content/uploads/2022/05/wxsync-2022-05-0acee72ee7dff1769adb35a3106775a2.png');background-repeat: no-repeat;background-size: 100% 100%;"><br  /></section></section></section><p><br mpa-from-tpl="t"  /></p><p><span style="font-size: 15px;orphans: 4;text-align: start;background-color: rgb(255, 255, 255);white-space: pre-wrap;font-family: monospace;">618来袭,虽然Python的电子书很多,<span style="white-space:pre-wrap;font-family: monospace;">但是经典的Python书籍还是要看纸质的书</span>,<span style="white-space:pre-wrap;font-family: monospace;">感觉这样才有踏实感,而且长时间眼睛阅读也会舒服一些。我推荐几本非常不错的Python</span>书单,都是居家旅行必备的书籍。</span></p><p><br  /></p><p><strong><span style="font-size: 15px;">Python修炼必读:</span></strong><br  /></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p><span style="font-size: 15px;">Python高级编程</span></p></li><li><p><span style="font-size: 15px;">Python Cookbook</span></p></li><li><p><span style="font-size: 15px;">流畅的Python</span></p></li><li><p><span style="font-size: 15px;">Python基础教程</span></p></li><li><p><span style="font-size: 15px;">精通正则表达式</span></p></li></ul><p><br  /></p><p><span style="font-size: 15px;"><strong><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">内功修炼:</span></strong></span></p><p><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;">代码大全,重构,CleanCode 这些经典的书籍必须要收藏先!</span><br  /></p><p><br  /></p><p line="vw8v" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">一起来薅当当的羊毛吧</p><p line="R3hn" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">全场自营图书每满100减50元 、满200元减100元</p><p line="T7Q3" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">在当当APP或者小程序结算时输入优惠码</p><p line="sm83" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;"><span style="color: rgb(255, 0, 0);">优惠码 (VZGVZE)</span> 满150再减30元</p><p line="init" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">花120元买300的书</p><p line="7lYu" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">优惠码有效期:6.9-6.20</p><p line="KBod" style="margin-top: 0pt;margin-bottom: 0pt;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.7;font-size: 11pt;color: rgb(73, 73, 73);text-align: center;">优惠码数量有限,先到先得哦</p><p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;text-align: center;"><br  /></p><p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;letter-spacing: 0.5px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;"></p><p style="margin-right: 8px;margin-left: 8px;letter-spacing: 0.544px;"><br  /></p><p style="margin-right: 8px;margin-left: 8px;letter-spacing: 0.544px;"><span style="color: rgb(255, 41, 65);font-size: 15px;"><strong><br  /></strong></span></p><p style="margin-right: 8px;margin-left: 8px;letter-spacing: 0.544px;"><span style="color: rgb(255, 41, 65);font-size: 15px;"><strong>近期热门:</strong></span></p><ul class="list-paddingleft-2" style="width: 577.422px;letter-spacing: 0.544px;"><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">墙裂推荐!B站上的Python学习资源</span><br  /></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">只要三步,带你用Python换脸</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">趣味!用Python竟然还能做一个文字套娃</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">只要三步,带你用Python换脸</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">一行Python代码写的游戏,我能这样玩一天!</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">让你纵横 GitHub 的五大神器</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">50行Python代码,一键获取微博热点</span></section></li><li style="font-size: 15px;"><section style="line-height: 2em;"><span style="font-size: 15px;">值得收藏!8大技巧,带你了解菜鸟和高手的区别!</span></section></li></ul>

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

你可能也喜欢

热评文章

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享