python函数缺省值与引用学习笔记分享 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    复制代码代码如下:

    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

    再下次调用时,如果继续参数空缺而使用缺省值,那么缺省值延续上次引用,

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

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

    复制代码代码如下:

    # -*- 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': ''}

    QQ空间 搜狐微博 人人网 开心网 百度搜藏更多

    Tags:函数缺省值

    复制链接收藏本文打印本文关闭本文返回首页

    上一篇:flask中使用SQLAlchemy进行辅助开发的代码

    下一篇:windows下wxPython开发环境安装与配置方法

   

相关文章

2013-12-12Mac OS X10.9安装的Python2.7升级Python3.3步骤详解

2014-01-01Python群发邮件实例代码

2011-02-02python实现的防DDoS脚本

2014-04-04解决python写的windows服务不能启动的问题

2009-09-09PHP webshell检查工具 python实现代码

2012-10-10Python天气预报采集器实现代码(网页爬虫)

2008-09-09Python 网络编程起步(Socket发送消息)

2014-04-04python实现DNS正向查询、反向查询的例子

2013-11-11python实现系统状态监测和故障转移实例方法

2014-03-03python基础教程之字典操作详解

   

文章评论

   

最 近 更 新

   

python缩进区别分析

Python多线程学习资料

python使用在线API查询IP对应的地理位置信

让python的Cookie.py模块支持冒号做key的

Python getopt模块处理命令行选项实例

python单线程实现多个定时器示例

Python logging模块学习笔记

使用python调用浏览器并打开一个网址的例

pydev使用wxpython找不到路径的解决方法

python逐行读取文件内容的三种方法

   

热 点 排 行

   

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

最新文章