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

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

问题描述

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

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

tester.py:

<前>类测试器():def __init__(self):self.mydict = self.test()def test(self,out={}):键 = "键"对于 ['a','b','c','d'] 中的 i:如果输入:out[key] += ','+i别的:出[键] =我回来

Python 提示:

<前>Python 2.6.6(r266:84292,2010 年 10 月 6 日,00:44:09)[GCC 4.2.1 (Apple Inc. build 5664)] 在达尔文上>>> 进口测试仪>>> t = tester.Tester()>>> 打印 t.mydict{'key': 'a,b,c,d'}>>> t2 = tester.Tester()>>> 打印 t2.mydict{'key': 'a,b,c,d,a,b,c,d'}

解决方案

这是一个几乎所有 Python 用户都遇到过一两次的功能.主要用途是缓存等,以避免重复冗长的计算(简单的记忆,真的),尽管我相信人们已经找到了它的其他用途.

这样做的原因是 def 语句只执行一次,也就是定义函数时.因此初始化值只被创建一次.对于像列表或字典这样的引用类型(而不是不能改变的不可变类型),这最终会成为一个明显且令人惊讶的陷阱,而对于值类型,它却被忽视了.

通常,人们是这样解决它的:

def test(a=None):如果 a 是 None:a = {}# ... 等等.

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.

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:

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 prompt:

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'}

解决方案

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.

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天全站免登陆