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实现生成简单的Makefile文件代码示例
Mar 10 Python
Python中取整的几种方法小结
Jan 06 Python
Python正则替换字符串函数re.sub用法示例
Jan 19 Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 Python
Django 在iframe里跳转顶层url的例子
Aug 21 Python
Django项目之Elasticsearch搜索引擎的实例
Aug 21 Python
Pytorch基本变量类型FloatTensor与Variable用法
Jan 08 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
Feb 27 Python
TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的
Apr 20 Python
Python2.6版本pip安装步骤解析
Aug 17 Python
python3.7.3版本和django2.2.3版本是否可以兼容
Sep 01 Python
Python爬虫之Selenium库的使用方法
Jan 03 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
shopex中集成的站长统计功能的代码简单分析
2011/08/11 PHP
php array_pop 删除数组最后一个元素实例
2016/11/02 PHP
PHP实现向关联数组指定的Key之前插入元素的方法
2017/06/06 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
2017/07/20 PHP
laravel withCount 统计关联数量的方法
2019/10/10 PHP
Extjs根据条件设置表格某行背景色示例
2014/07/23 Javascript
javascript中caller和callee详解
2015/08/10 Javascript
jquery图片轮播特效代码分享
2020/04/20 Javascript
js时间戳转为日期格式的方法
2015/12/28 Javascript
APP中javascript+css3实现下拉刷新效果
2016/01/27 Javascript
JS 获取HTML标签内的子节点的方法
2016/09/21 Javascript
利用Vue.js实现checkbox的全选反选效果
2017/01/18 Javascript
jQuery EasyUI Accordion可伸缩面板组件使用详解
2017/02/28 Javascript
jQuery列表检索功能实现代码
2017/07/17 jQuery
jQuery实现所有验证通过方可提交的表单验证
2017/11/21 jQuery
JavaScript防止全局变量污染的方法总结
2018/08/02 Javascript
vue select选择框数据变化监听方法
2018/08/24 Javascript
微信小程序录音实现功能并上传(使用node解析接收)
2020/02/26 Javascript
Vue的data、computed、watch源码浅谈
2020/04/04 Javascript
Vue中key的作用示例代码详解
2020/06/10 Javascript
Python multiprocessing.Manager介绍和实例(进程间共享数据)
2014/11/21 Python
Python中字典(dict)合并的四种方法总结
2017/08/10 Python
django实现登录时候输入密码错误5次锁定用户十分钟
2017/11/05 Python
pandas.DataFrame选取/排除特定行的方法
2018/07/03 Python
Flask框架学习笔记之消息提示与异常处理操作详解
2019/08/15 Python
Python面向对象封装操作案例详解
2019/12/31 Python
Python利用全连接神经网络求解MNIST问题详解
2020/01/14 Python
python pandas利用fillna方法实现部分自动填充功能
2020/03/16 Python
HTML5 Canvas 起步(1) - 基本概念
2009/05/12 HTML / CSS
台湾乐天市场:日本No.1的网路购物网站
2017/03/22 全球购物
大学生的四年学习自我评价
2013/12/13 职场文书
最新的咖啡店创业计划书
2013/12/30 职场文书
高中生思想道德自我评价
2015/03/09 职场文书
会议室使用管理制度
2015/08/06 职场文书
如何书写民事调解协议书?
2019/06/25 职场文书
Apache Hudi数据布局黑科技降低一半查询时间
2022/03/31 Servers