classification
Title: Object deletion and re-creation points to same attribute if attribute is a dictionary.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Nitin Bhojwani, benjamin.peterson
Priority: normal Keywords:

Created on 2018-05-31 04:18 by Nitin Bhojwani, last changed 2018-05-31 04:36 by benjamin.peterson. This issue is now closed.

Files
File name Uploaded Description Edit
test-bug.zip Nitin Bhojwani, 2018-05-31 04:18 The zip file contains ngraph.py which defines a class and test_util.py which runs test against it.
Messages (2)
msg318241 - (view) Author: Nitin Bhojwani (Nitin Bhojwani) * Date: 2018-05-31 04:18
I observed a strange behaviour while working on a module.
This bug is present in multiple python version so seems it is related to core-interpreter.

Behaviour can be reproduced by:
1. Create an object and in __init__ of class, we define a dictionary attribute to that object.
2. After that we change that attribute, like assign any key-value(s) to that attribute.
3. Delete the object.
4. recreate the object.
5. If you check, this new object's same attribute(dictionary) is same old attribute which was pointed by the older object.

This is a behavioural bug, as once delete is performed on an object the whole object and it's (attributes') references must be removed.

I can demonstrate this using a test from unittest, in which I put instance creation in setUp phase, instance deletion in tearDown phase and run same test multiple times. The result is different each time.

The zip file contains ngraph.py which defines a class and test_util.py which runs test against it.
Unzip and execute as:

python test_util.py #2.7.10
python3 test_util.py #3.6.3
msg318246 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-05-31 04:36
Please see https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects
History
Date User Action Args
2018-05-31 04:36:35benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg318246

resolution: not a bug
stage: resolved
2018-05-31 04:18:48Nitin Bhojwanicreate