为什么通过python默认变量初始化变量会在对象实例化后保持状态? [英] Why does initializing a variable via a python default variable keep state across object instantiation?

查看:370
本文介绍了为什么通过python默认变量初始化变量会在对象实例化后保持状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了一个有趣的python错误,其中反复实例化一个类似乎处于保持状态.在以后的实例化调用中,变量已经定义.

I hit an interesting python bug today in which instantiating a class repeatedly appears to be holding state. In later instantiation calls the variable is already defined.

我将问题归结为以下类/shell交互.我意识到这不是初始化类变量的最佳方法,但是肯定不应该这样.这是一个真正的错误还是功能"? :D

I boiled down the issue into the following class/shell interaction. I realize that this is not the best way to initialize a class variable, but it sure should not be behaving like this. Is this a true bug or is this a "feature"? :D

tester.py:

tester.py:


class Tester():
        def __init__(self):
                self.mydict = self.test()

        def test(self,out={}):
                key = "key"
                for i in ['a','b','c','d']:
                        if key in out:
                                out[key] += ','+i
                        else:   
                                out[key] = i 
                return out

Python提示:


Python 2.6.6 (r266:84292, Oct  6 2010, 00:44:09) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
>>> import tester
>>> t = tester.Tester()
>>> print t.mydict
{'key': 'a,b,c,d'}
>>> t2 = tester.Tester()
>>> print t2.mydict
{'key': 'a,b,c,d,a,b,c,d'}

推荐答案

该功能几乎所有Python用户都会遇到一次或两次.主要用途是用于缓存等,以避免重复的冗长计算(实际上是简单的记忆),尽管我确信人们已经找到了其他用途.

It is a feature that pretty much all Python users run into once or twice. The main usage is for caches and the like to avoid repetitive lengthy calculations (simple memoizing, really), although I am sure people have found other uses for it.

这样做的原因是def语句仅在定义函数时执行一次.因此,初始化值仅创建一次.对于像列表或字典这样的引用类型(相对于不能更改的不可变类型),这最终会导致可见和令人惊讶的陷阱,而对于值类型,它却不会引起注意.

The reason for this is that the def statement only gets executed once, which is when the function is defined. Thus the initializer value only gets created once. For a reference type (as opposed to an immutable type which cannot change) like a list or a dictionary, this ends up as a visible and surprising pitfall, whereas for value types, it goes unnoticed.

通常,人们会像这样解决它:

Usually, people work around it like this:

def test(a=None):
    if a is None:
        a = {}
    # ... etc.

这篇关于为什么通过python默认变量初始化变量会在对象实例化后保持状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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