python3 之初学者常犯的5个错误
1. Creating a copy of dictionary or lists.
Whenever you need to make a copy of a dictionary or list, do not simply use the assignment operator.
Right way: Use the copy()
or deepcopy()
method.
2. Dictionary keys.
Remember Boolean class is the subclass of Integer. Integer equivalent of True is 1 and that of False is 0. Hence value of key 1 is overwritten
3. Updating lists or dictionaries.
Most the sequence object methods like sort, update, append, add etc works in place to increase performance by avoiding to create a separate copy un-necessarily.
Do not try to assign the output of such methods to variable.
In some cases, python try to reuse the existing immutable objects. String interning is one such case
>>> a = "gmail"
>>> b = "gmail"
>>> a is b
True
All strings of length 1 are interned. String having anything except ASCII characters, digits and underscore in them are not interned.
Let's see.
>>> a = "@gmail"
>>> b = "@gmail"
>>> a is b
False
Also remember==
is different thanis
operator.==
checks if values are equal or not whileis
checks if both variable are pointing to same object.
5. Default arguments are evaluated only once.
Consider below example.
def func(a, lst=[]):
lst.append(a)
return lst print(func(1))
print(func(2))
What do you think will be the output of above two print statements?
Lets try to run it.
>>> def func(a, lst=[]):
... lst.append(a)
... return lst
...
>>> print(func(1))
[1]
>>> print(func(2))
[1, 2]
python3 之初学者常犯的5个错误的更多相关文章
- Python开发者最常犯的10个错误
Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库.与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义语句块. 在平时的工作中,Python开发者很容易犯一些小错 ...
- Spring常犯的十大错误,你踩过吗?
1.错误一:太过关注底层 我们正在解决这个常见错误,是因为 "非我所创" 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理解特定库的 ...
- Web开发人员常犯的10个错误
说到开发一个运行在现代网络中的网站:Web开发人员需要选择虚拟主机平台和底层数据存储,准备编写HTML.CSS和JavaScript用的工具,要有设计执行方式,以及一些可用的JavaScript库/框 ...
- Java程序员常犯的10个错误
本文总结了Java程序员常犯的10个错误. #1. 把Array转化成ArrayList 把Array转化成ArrayList,程序员经常用以下方法: List<String> lis ...
- OI中常犯的傻逼错误总结
OI中常犯的傻逼错误总结 问题 解决方案 文件名出错,包括文件夹,程序文件名,输入输出文件名 复制pdf的名字 没有去掉调试信息 调试时在后面加个显眼的标记 数组开小,超过定义大小,maxn/ ...
- python开发者常犯的10个错误(转)
常见错误1:错误地将表达式作为函数的默认参数 在Python中,我们可以为函数的某个参数设置默认值,使该参数成为可选参数.虽然这是一个很好的语言特性,但是当默认值是可变类型时,也会导致一些令人困惑的情 ...
- Spring 常犯的十大错误,答应我 打死都不要犯好吗?
1. 错误一:太过关注底层 我们正在解决这个常见错误,是因为 “非我所创” 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理解特定库的内部结构及其实现, ...
- Spring 常犯的十大错误,(收藏后)永远不要在犯了
1. 错误一:太过关注底层 我们正在解决这个常见错误,是因为 “非我所创” 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理解特定库的内部结构及其实现, ...
- Spring 常犯的十大错误,这坑你踩过吗?
阅读本文大概需要 9 分钟. 1.错误一:太过关注底层 我们正在解决这个常见错误,是因为 “非我所创” 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理 ...
随机推荐
- linux基础三---网络基础&软件包管理
一 ifconfig:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址. ifconfig eno16777736 down/up 关闭/开启 eno16777736 网卡 ifconf ...
- gem Errno::ECONNRESET: Connection reset by peer - SSL_connect
问题描述 在使用gem安装软件包时,会时常遇到下面的问题: ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError) Errno: ...
- redis 数据导入导出,实例内db迁移
源实例db0迁移至目标实例db1 [root@172.20.0.1 ~]# cat redis_mv.sh #!/bin/bash redis-cli -h -a password -n keys & ...
- centos6 多段Ip添加脚本
#!/bin/bash export device=`ifconfig|grep eth0|head -n 1|awk '{print ($1)}'`export ipcfg_pre="/e ...
- VC 模拟CMD 匿名管道
#include "stdafx.h" #include <Windows.h> #include <stdio.h> #include <stdli ...
- tyvj 1057 金明的预算方案 背包dp
P1057 金明的预算方案 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2006 提高组 第二道 描述 金明今天很开心,家里购置的新房就要领钥匙了 ...
- Java 创建线程的两种方法
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...
- JMeter正则表达式提取器说明
Apply to:应用范围 要检查的响应字段:样本数据源. 引用名称:其他地方引用时的变量名称,引用方法:${引用名称} 正则表达式:数据提取器,如上图的 "sysNo":&quo ...
- dede数据库表结构和dedecms数据库字段说明
表名:dede_addonarticle (ENGINE=MyISAM/CHARSET=gbk) 说明:Top 字段名 说明描述 具体参数 aid 文章ID mediumint(8) unsigned ...
- 防止php重复提交表单更安全的方法
Token.php <?php /* * Created on 2013-3-25 * * To change the template for this generated file go t ...