This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Empty Dict in Initializer is Shared Betwean Objects
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mattofak, pitrou
Priority: normal Keywords:

Created on 2012-05-08 16:48 by mattofak, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg160212 - (view) Author: Matthew Walker (mattofak) Date: 2012-05-08 16:48
When initializing a class with an empty dict() object as a default initializer, if it is not overridden, multiple instances of the class will share the dictionary. IE:

class test(object):
  def __init__(self, obj=dict()):
    self.obj = obj

a = test()
b = test()

Then id(a.obj) points to the same location as id(b.obj). The behaviour I would expect would be that a.obj and b.obj would be unique instances.
msg160213 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-05-08 16:49
This is not a bug, see http://docs.python.org/dev/faq/design.html#why-are-default-values-shared-between-objects
History
Date User Action Args
2022-04-11 14:57:30adminsetgithub: 58961
2012-05-08 16:49:44pitrousetstatus: open -> closed
2012-05-08 16:49:39pitrousetnosy: + pitrou
messages: + msg160213

resolution: not a bug
stage: resolved
2012-05-08 16:48:55mattofaksettype: behavior
2012-05-08 16:48:00mattofakcreate