python tips - 注意函数参数的默认值-默认参数-可选参数
来源:互联网 发布:大数据平台架构师 编辑:程序博客网 时间:2024/05/17 06:49
且看如下例子:
def f(a=[]): a.append(0) return aprint dir(f)for i in range(3): print f() print f.__defaults__
结果:
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']([],)[0]([0],)[0, 0]([0, 0],)[0, 0, 0]
函数参数的默认值会保存上次调用的修改么?
每运行一次,函数作为一个对象的内在属性的值发生了变化。导致每次运行的结果不一致
def f(num=0): num += 1 return numprint dir(f)for i in range(3): print f.__defaults__ print f()
结果:
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'](0,)1(0,)1(0,)1
函数参数的默认值怎么又没有保存上次调用的修改呢?
参考:python tips - Python可变对象和不可变对象:http://www.cnblogs.com/congbo/archive/2012/11/20/2777031.html
使用可变参数作为默认值可能导致意料之外的行为。为了防止出现这种情况,最好使用None值,并且在后面加上检查代码,如示例1改为:
def f(a=None): if a is None: a = [] a.append(0) return a
查阅Python manual有如下的说法:
Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.:
python中函数的默认值只会被执行一次,(和静态变量一样,静态变量初始化也是被执行一次)。Python可以通过函数的默认值来实现静态变量的功能。
- python tips - 注意函数参数的默认值-默认参数-可选参数
- python 函数参数(必选参数、默认参数、可选参数、关键字参数)
- python 函数参数(必选参数、默认参数、可选参数、关键字参数)
- python函数参数默认值
- 函数的可选参数
- 命名参数、可选参数和默认值参数
- [Python]默认函数参数注意点
- 函数参数的默认值
- 函数的参数默认值
- python的函数 参数的默认值
- Python函数参数默认值的陷阱
- C++ tips: 类成员函数的参数默认值
- Python函数的默认参数
- 默认参数 参数默认值 二三事
- JavaScript:函数的可选参数
- PHP函数的可选参数
- 函数的默认值+带默认参数的函数
- 函数的默认值与带默认参数的函数
- golang的小总结
- 欢迎使用CSDN-markdown编辑器
- 一位软件工程师的6年总结
- 使用android studio导入之前版本系统的project sync failed 解决办法
- 杭电 1015 Safecracker
- python tips - 注意函数参数的默认值-默认参数-可选参数
- QML之javascript
- 尼泊尔部分地方成"孤岛" 死亡人数或过万
- Android之圆形(圆角)图片的实现(一)
- 免费DNS解析服务:
- linux命令英文缩写的含义(方便记忆)
- GAEA Winlog v4.50 1CD(创建钻井日志和测井曲线的工具) GAEA Winsieve v1.2 1CD(快速输入和打印结晶粒度分析曲线)
- Jlink_V8灯不亮USB无法识别及克隆版问题解决方法(win7 64位系统下亲测可用)
- 我的Java学习推荐书目