仍然对可变的默认参数值“陷阱"感到困惑.在Python中 [英] Still confused about mutable default parameter value "gotcha" in Python

查看:49
本文介绍了仍然对可变的默认参数值“陷阱"感到困惑.在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解不应使用

解决方案

在您看来,执行结束时,默认值x却被丢弃了.

事实上,Python有一个全局名称空间,其中包含所有可供使用的名称(内置函数,导入或定义的类和函数).

此命名空间的内容由对象组成.功能也是对象.

作为测试,如果您在脚本或python命令行中尝试此操作,您将明白我的意思:

  def f(x = [1,2,3]):x.append(4)打印(x)打印目录(f) 

您将看到函数f的对象性质.作为对象,默认值是在属性 f.func_defaults 中引用的,因此它们始终可用,并且如果可变,它们会保留更改,从而给您带来副作用.

在python 3中,该属性已由 f .__ defaults __

替换

I understand that one should not use mutable default parameter value in Python (with some exceptions) because this value is evaluated and stored only once when the function is defined, and not each time the function is later called.

My understanding of that is this (using the example below; please excuse my imprecise language as I'm just a beginner in Python programming, who's stuck at the Function chapter of my textbook because of this):

def f(x = [1, 2, 3]):
    x.append(4)
    print(x)

f()
f()

1) The function f is defined, and x (local variable in f) assumes the default variable of [1, 2, 3] (even before the function is called)

2) When f() is called, x is still [1, 2, 3] due to no argument passed to it, and x continues having its default value

3) x is modified in place with append, becomes [1, 2, 3, 4], and is printed as such

However, this is where my confusion arises. I'd assume that:

4) When f ends, x is destroyed (in the stack or whatever you'd call it) and is no longer associated with the list object [1, 2, 3, 4]**

5) The list object [1, 2, 3, 4] is reclaimed since there's no variable that refers to it anymore

Therefore,

6) When f() is called the second time, I'd expect Python to output an error since x now no longer has a value associated with it. In other words, how can Python reuse the default value from the last evaluation when it's been reclaimed/destroyed?

Appreciate all your help and explanation!

** this understanding I got from Ned Batchelder's page on variable name assignment (see below)

解决方案

While it may seems to you that at the end of the execution x, the default value, is disposed, it is not.

In fact, Python has a global namespace with all the names available for you to use (built-in functions, classes and functions you import or define).

The content of this namespace is made of objects. Function are objects too.

As a test, if you try this in a script or in the python command line, you will see what I mean:

def f(x = [1, 2, 3]):
    x.append(4)
    print(x)
print dir(f)

you will see the object nature of the function f. As an objects, the default values are referenced in an attribute, f.func_defaults, therefore they are always available and if mutable they retain the changes, giving you side effects with you may not want.

EDIT: in python 3 the attribute has been replaced by f.__defaults__

这篇关于仍然对可变的默认参数值“陷阱"感到困惑.在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆