Python Forum
An interesting BUG with double assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An interesting BUG with double assignment
#1
I came across the following bug
D = dict()
D = D['spam'] = dict()
I expected that D['spam'] = dict() would create a new dictionary as a value in D, then the D = part would reassign the variable D as this new empty dictionary.

But this is not what happened! Here is a more detailed version
>>> 
>>> D = dict()
>>> X = D
>>> D = D['spam'] = dict()
>>> D
{'spam': {...}}    # Hey! dictionary D is not empty. It has a key 'spam'
>>> D is D['spam']     # What? D['spam'] is D itself! D is a cyclic object that contains itself
True
>>> X is D
False
>>> X
{}     # X is still the empty dictionary that we created at the beginning
Explanation
The dis module explains what's going on
>>> import dis
>>> dis.dis('D = D["spam"] = dict()')
  1           0 LOAD_NAME                0 (dict)
              2 CALL_FUNCTION            0            # The dict() function is called and its result is on top of the stack
              4 DUP_TOP                                       # The top of the stack is duplicated!
              6 STORE_NAME               1 (D)        # the top element is consumed and assigned to D: from left to right
              8 LOAD_NAME                1 (D)
             10 LOAD_CONST               0 ('spam')
             12 STORE_SUBSCR                         # the duplicated top element is consumed and assigned to D['spam']
             14 LOAD_CONST               1 (None)
             16 RETURN_VALUE
>>> 
Conclusion: when we write
a = b = c = d = value
it is equivalent, in this order to
a = value
b = value
c = value
d = value
When b is a['spam'] it is the new a that is used for the second assignment!

What should I have done?
D['spam'] = D = dict()
Proof
>>> D = dict()
>>> X = D
>>> D['spam'] = D = dict()
>>> D
{}
>>> X
{'spam': {}}
>>> X['spam'] is D
True
Reply
#2
D['spam'] = D = dict()
The last assignment wins. The last assignment is an empty dict.

EDIT: End it seems that dict() is assigned to D and not to D["spam"]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Oct-08-2023, 01:03 PM)DeaD_EyE Wrote: EDIT: End it seems that dict() is assigned to D and not to D["spam"]
Yes it is. The assignment does the same as
value = dict()
D['spam']=value
D = value
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  most interesting or useful modules? t4keheart 7 3,429 Jan-20-2020, 06:03 AM
Last Post: wavic
  Interesting/Fun Scripts? ejected 2 2,389 Mar-27-2019, 07:49 AM
Last Post: Skaperen
  Interesting article on PyTourch Larz60+ 0 2,287 Jan-03-2018, 09:06 PM
Last Post: Larz60+
  Any interesting works in progress? mpd 5 3,763 Dec-15-2017, 09:34 PM
Last Post: wavic
  An interesting blog wavic 0 2,393 Dec-12-2017, 09:09 PM
Last Post: wavic
  interesting gotcha Skaperen 11 6,518 Nov-30-2017, 03:53 AM
Last Post: wavic
  Interesting new features in python3 Kebap 8 5,901 Jul-05-2017, 03:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020