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中移动目录结构的方法
Jan 31 Python
Python中死锁的形成示例及死锁情况的防止
Jun 14 Python
浅谈python迭代器
Nov 08 Python
分享Pycharm中一些不为人知的技巧
Apr 03 Python
python 自动去除空行的实例
Jul 24 Python
python实现自动化上线脚本的示例
Jul 01 Python
python3连接MySQL8.0的两种方式
Feb 17 Python
PYQT5 vscode联合操作qtdesigner的方法
Mar 24 Python
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
Sep 28 Python
python如何控制进程或者线程的个数
Oct 16 Python
matplotlib之pyplot模块之标题(title()和suptitle())
Feb 22 Python
Python 线程池模块之多线程操作代码
May 20 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实现的MongoDB数据库操作类分享
2014/05/12 PHP
php从数组中随机选择若干不重复元素的方法
2015/03/14 PHP
CentOS下PHP7的编译安装及MySQL的支持和一些常见问题的解决办法
2015/12/17 PHP
Yii中CGridView实现批量删除的方法
2015/12/28 PHP
PHP时间相关常用函数用法示例
2020/06/03 PHP
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
教你如何使用PHP输出中文JSON字符串
2014/05/22 Javascript
使用JS轻松实现ionic调用键盘搜索功能(超实用)
2016/09/06 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
Vue.js 利用v-for中的index值实现隔行变色
2018/08/01 Javascript
深入理解react-router 路由的实现原理
2018/09/26 Javascript
node版本管理工具n包使用教程详解
2018/11/09 Javascript
JavaScript中的"=、==、==="区别讲解
2019/01/22 Javascript
vue 实现cli3.0中使用proxy进行代理转发
2019/10/30 Javascript
基于Vue中的父子传值问题解决
2020/07/27 Javascript
jquery自定义组件实例详解
2020/12/31 jQuery
python使用os模块的os.walk遍历文件夹示例
2014/01/27 Python
Python和JavaScript间代码转换的4个工具
2016/02/22 Python
Python中的sort()方法使用基础教程
2017/01/08 Python
Python中第三方库Requests库的高级用法详解
2017/03/12 Python
TensorFlow实现卷积神经网络CNN
2018/03/09 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
浅析Python 简单工厂模式和工厂方法模式的优缺点
2020/07/13 Python
Pycharm同步远程服务器调试的方法步骤
2020/11/04 Python
婚鞋、新娘鞋、礼服鞋、童鞋:Nina Shoes
2019/09/04 全球购物
Ajax的工作原理
2015/12/04 面试题
函授毕业生自我鉴定
2013/11/06 职场文书
怎样写演讲稿
2014/01/04 职场文书
普罗米修斯教学反思
2014/02/06 职场文书
端午节演讲稿
2014/05/23 职场文书
党员自我剖析材料范文
2014/10/06 职场文书
高三毕业评语
2014/12/31 职场文书
同学聚会通知书
2015/04/20 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
Python中快速掌握Data Frame的常用操作
2021/03/31 Python