python函数缺省值与引用学习笔记分享


Posted in Python onFebruary 10, 2013
import random, string
class C(object):    pass
def dangerFunction(msg, l = [], b = {}, c = C()):
    print msg, '-'*10
    print l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print l, b, c.__dict__
dangerFunction('1')
dangerFunction('2')
dangerFunction('3')
print '-'*20
def safeFunction(msg, l = None, b = None, c = None):
    if not l:   l = []
    if not b:   b = {}
    if not c:   c = C()
    print msg, '-'*10
    print l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print l, b, c.__dict__
safeFunction('1')
safeFunction('2')
safeFunction('3')

运行结果:
1 ----------
[] {} {}
[1] {'q': ''} {'p': ''}
2 ----------
[1] {'q': ''} {'p': ''}
[1, 1] {'q': '', 'a': ''} {'p': '', 'g': ''}
3 ----------
[1, 1] {'q': '', 'a': ''} {'p': '', 'g': ''}
[1, 1, 1] {'q': '', 'a': '', 'w': ''} {'p': '', 'w': '', 'g': ''}
--------------------
1 ----------
[] {} {}
[1] {'k': ''} {'l': ''}
2 ----------
[] {} {}
[1] {'r': ''} {'c': ''}
3 ----------
[] {} {}
[1] {'q': ''} {'h': ''}

由dangerFunction打印出来的结果来看,缺省值为 [], (), class
再下次调用时,如果继续参数空缺而使用缺省值,那么缺省值延续上次引用。

可能打印无任何标志无法看清楚,加上文字应该会简单很多。

# -*- coding: utf-8 -*-
import random, string
class C(object):    pass
def dangerFunction(msg, l = [], b = {}, c = C()):
    print msg, '-'*10
    print u'操作前', l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print u'操作后', l, b, c.__dict__
dangerFunction('1')
dangerFunction('2')
dangerFunction('3')
print '-' * 10, u'我是分隔符', '-' * 10
def safeFunction(msg, l = None, b = None, c = None):
    if not l:   l = []
    if not b:   b = {}
    if not c:   c = C()
    print msg, '-'*10
    print u'操作前', l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print u'操作后',l, b, c.__dict__
safeFunction('1')
safeFunction('2')
safeFunction('3')
1 ----------
操作前 [] {} {}
操作后 [1] {'m': ''} {'v': ''}
2 ----------
操作前 [1] {'m': ''} {'v': ''}
操作后 [1, 1] {'i': '', 'm': ''} {'g': '', 'v': ''}
3 ----------
操作前 [1, 1] {'i': '', 'm': ''} {'g': '', 'v': ''}
操作后 [1, 1, 1] {'i': '', 's': '', 'm': ''} {'s': '', 'g': '', 'v': ''}
---------- 我是分隔符 ----------
1 ----------
操作前 [] {} {}
操作后 [1] {'e': ''} {'q': ''}
2 ----------
操作前 [] {} {}
操作后 [1] {'d': ''} {'s': ''}
3 ----------
操作前 [] {} {}
操作后 [1] {'m': ''} {'k': ''}
Python 相关文章推荐
Python求导数的方法
May 09 Python
深入理解Python变量与常量
Jun 02 Python
python中将字典形式的数据循环插入Excel
Jan 16 Python
Django中如何防范CSRF跨站点请求伪造攻击的实现
Apr 28 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
python的scipy实现插值的示例代码
Nov 12 Python
python绘制BA无标度网络示例代码
Nov 21 Python
iPython pylab模式启动方式
Apr 24 Python
Python使用正则表达式实现爬虫数据抽取
Aug 17 Python
Python datetime 如何处理时区信息
Sep 02 Python
Django利用elasticsearch(搜索引擎)实现搜索功能
Nov 26 Python
python实现学员管理系统(面向对象版)
Jun 05 Python
flask中使用SQLAlchemy进行辅助开发的代码
Feb 10 #Python
用python实现的去除win下文本文件头部BOM的代码
Feb 10 #Python
python创建只读属性对象的方法(ReadOnlyObject)
Feb 10 #Python
python将人民币转换大写的脚本代码
Feb 10 #Python
pydev使用wxpython找不到路径的解决方法
Feb 10 #Python
python的id()函数介绍
Feb 10 #Python
python client使用http post 到server端的代码
Feb 10 #Python
You might like
PHP 和 MySQL 基础教程(一)
2006/10/09 PHP
js Function类型
2011/12/04 Javascript
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
浅析tr的隐藏和显示问题
2014/03/05 Javascript
nodejs教程之环境安装及运行
2014/11/21 NodeJs
js根据鼠标移动速度背景图片自动旋转的方法
2015/02/28 Javascript
JS获取网页图片name属性的方法
2015/04/01 Javascript
js弹出窗口简单实现代码
2017/03/22 Javascript
easyui-datagrid特殊字符不能显示的处理方法
2017/04/12 Javascript
详解Vue学习笔记入门篇之组件的内容分发(slot)
2017/07/17 Javascript
bootstrap fileinput实现文件上传功能
2017/08/23 Javascript
jQuery实现获取选中复选框的值实例详解
2018/06/28 jQuery
JS实现简单的抽奖转盘效果示例
2019/02/16 Javascript
使用vue for时为什么要key【推荐】
2019/07/11 Javascript
layer.open 获取不到表单信息的解决方法
2019/09/26 Javascript
Node.js中console.log()输出彩色字体的方法示例
2019/12/01 Javascript
[34:39]Secret vs VG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
python中的lambda表达式用法详解
2016/06/22 Python
python设置值及NaN值处理方法
2018/07/03 Python
python实现随机漫步算法
2018/08/27 Python
Python创建空列表的字典2种方法详解
2020/02/13 Python
Python 如何实现访问者模式
2020/07/28 Python
CSS3中Animation属性的使用详解
2015/08/06 HTML / CSS
世界领先的高品质定制产品平台:Zazzle
2017/07/23 全球购物
ECCO爱步官方旗舰店:丹麦鞋履品牌
2018/01/02 全球购物
Conforama西班牙:您的家具、装饰和电器商店
2020/02/21 全球购物
通信工程求职信
2014/07/16 职场文书
意向书范本
2014/07/29 职场文书
万能检讨书2000字
2014/10/17 职场文书
学校运动会加油词
2015/07/18 职场文书
运动会3000米加油稿
2015/07/21 职场文书
2019通用版新员工入职培训方案!
2019/07/11 职场文书
MATLAB 如何求取离散点的曲率最大值
2021/04/16 Python
k-means & DBSCAN 总结
2021/04/27 Python
python3.7.2 tkinter entry框限定输入数字的操作
2021/05/22 Python
Apache POI的基本使用详解
2021/11/07 Servers