python 易出错总结
来源:互联网 发布:数据库账号管理办法 编辑:程序博客网 时间:2024/05/17 08:33
1.Default Parameter Values
This is a common mistake that beginners often make. Even more advanced programmers make this mistake if they don't understand Python names.
def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list
The problem here is that the default value of a_list, an empty list, is evaluated at function definition time. So every time you call the function, you get thesame default value. Try it several times:
>>> print bad_append('one')['one']
>>> print bad_append('two')['one', 'two']
Lists are a mutable objects; you can change their contents. The correct way to get a default list (or dictionary, or set) is to create it at run time instead,inside the function:
def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list
2.
- python 易出错总结
- STM32 外部中断 易出错总结
- STM32 外部中断 易出错总结
- python连接sqlite3出错
- python出错解决
- python 编码出错问题
- python Image库 出错
- python写文件出错
- [Python]import 出错
- python 导入模块出错
- python import模块出错
- Python使用selenium出错
- python字符串编码出错
- mysql-python安装出错
- Windows连接出错总结
- sqlserver出错总结
- SQL出错总结
- Socket连接出错总结
- 浅谈Jsch &&SSH2
- 短彩和vcard结合
- spring security 使用总结
- c++面试题-vector手写实现版本
- 中国十大IC设计公司和十大最有潜力IC公司2010
- python 易出错总结
- maven安装及配置
- c#创建多项目解决方案时遇到解决方案消失的解决办法
- mvc路由实现html伪静态后,正常的html无法打开,解决
- Wise安装制作
- 又见一帘幽梦
- QEMU安装使用全攻
- win+Apache+mod_wsgi部署python程序-apache2.2+python2.7
- lnmp安装包出现Nginx 502 Bad Gateway错误解决方案