[New-bugs-announce] [issue39556] Different objects of the same class references the same dictionary

Kevin Young report at bugs.python.org
Wed Feb 5 01:35:54 EST 2020


New submission from Kevin Young <kings988 at gmail.com>:

Test code:

class Test(object):
    def __init__(self, a={}):
        self._a = a
    
    def put(self, k, v):
        self._a[k] = v

if __name__ == '__main__':
    t1 = Test()
    t1.put('aa', '11')
    t1.put('bb', '22')
    
    t2 = Test()
    t2.put('cc', '33')
    for k, v in t2._a.items():
        print(k, '=', v)

Output:
aa = 11
bb = 22
cc = 33

The expected output should be:
cc = 33

My workaround:
self._a = dict(a)

I have tested on both Python 3.7.3 and 3.8.1, they share the same results.
I'm not sure if this is a bug or on-purpose feature of python. Could someone provide some guidance for me? Thank you.

----------
components: Interpreter Core
messages: 361407
nosy: Kevin Young
priority: normal
severity: normal
status: open
title: Different objects of the same class references the same dictionary
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39556>
_______________________________________


More information about the New-bugs-announce mailing list