What causes this strange result in Python when assigning to values in a dictionary?

Kieran Hunt

I recently reduced a bug in some code to being a result of the following behaviour:

>>> arr = np.zeros(10)
>>> value = 0
>>> dictionary = {"key":[arr,value]}
>>> dictionary["key"][0]
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> dictionary["key"][1]
0
>>> dictionary["key"][0]+=1
>>> dictionary["key"][1]+=1
>>> dictionary["key"][0]   
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> dictionary["key"][1]   
1
>>> arr
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> value
0

Resulting in:

>>> dictionary["key"][0] is arr
True
>>> dictionary["key"][1] is value 
False

Probably a silly question, but what causes this?

Ffisegydd

In the case of the numpy array, you have the same object assigned with the name arr and inside your dictionary. As such, when you modify it you see the change appear for both, they're the same object after all, and numpy arrays are mutable.

For the integer, when you do dictionary["key"][1]+=1 you are creating a new integer inside the dictionary, this is because integers are immutable [1]. This means that the two integers (value and dictionary["key"][1]) are different objects, and so one is modified but the other is not.

[1] It might look like x = 2; x += 1 is operating on the same object, after all += should do "in-place" operations, but it's not because of the immutability. Behind the scenes you're actually re-binding x with a new integer object, as such they have the same name, but are different objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What causes this strange result in Python when assigning to values in a dictionary?

From Dev

What python concept causes this result?

From Dev

Assigning multiple values to a dictionary key- Python

From Dev

MVC - Assigning Values to Dictionary

From Dev

Split python dictionary to result in all combinations of values

From Dev

Assigning values to keys in a mutable dictionary

From Dev

Assigning values to an array after malloc causes crash

From Dev

Strange error using .values on a Dictionary

From Dev

when tokenize arabic text with python I get strange result?

From Dev

Python Sometimes Returns Strange Result When Reading HTML from URL

From Dev

Strange behaviour when assigning a String to a String variable

From Dev

Strange error when assigning a byte array

From Dev

Assigning an input variable to python dictionary

From Dev

Assigning function to a dictionary key - Python

From Dev

Python: Weird error when assigning arg values to variables

From Dev

Spacing when printing values in a python dictionary

From Dev

What causes this extremely strange Port Forward behaviour?

From Dev

Strange result when trying to print

From Dev

Crash when assigning values to array

From Dev

What is the result of assigning a ULONGLONG to a 16 byte array?

From Dev

In Python, what is a minimal way of assigning a value to a variable depending on whether a dictionary contains a key?

From Dev

Repeating captures in Python strange result

From Dev

Strange result with Python decimal module

From Dev

Python dictionary key error when assigning - how do I get around this?

From Dev

Assigning Values to an Array with for Loop Python

From Dev

assigning values to individual string python

From Dev

Assigning values in python, results not as expected

From Dev

Assigning randint values to objects [Python]

From Dev

What causes these default array values?

Related Related

  1. 1

    What causes this strange result in Python when assigning to values in a dictionary?

  2. 2

    What python concept causes this result?

  3. 3

    Assigning multiple values to a dictionary key- Python

  4. 4

    MVC - Assigning Values to Dictionary

  5. 5

    Split python dictionary to result in all combinations of values

  6. 6

    Assigning values to keys in a mutable dictionary

  7. 7

    Assigning values to an array after malloc causes crash

  8. 8

    Strange error using .values on a Dictionary

  9. 9

    when tokenize arabic text with python I get strange result?

  10. 10

    Python Sometimes Returns Strange Result When Reading HTML from URL

  11. 11

    Strange behaviour when assigning a String to a String variable

  12. 12

    Strange error when assigning a byte array

  13. 13

    Assigning an input variable to python dictionary

  14. 14

    Assigning function to a dictionary key - Python

  15. 15

    Python: Weird error when assigning arg values to variables

  16. 16

    Spacing when printing values in a python dictionary

  17. 17

    What causes this extremely strange Port Forward behaviour?

  18. 18

    Strange result when trying to print

  19. 19

    Crash when assigning values to array

  20. 20

    What is the result of assigning a ULONGLONG to a 16 byte array?

  21. 21

    In Python, what is a minimal way of assigning a value to a variable depending on whether a dictionary contains a key?

  22. 22

    Repeating captures in Python strange result

  23. 23

    Strange result with Python decimal module

  24. 24

    Python dictionary key error when assigning - how do I get around this?

  25. 25

    Assigning Values to an Array with for Loop Python

  26. 26

    assigning values to individual string python

  27. 27

    Assigning values in python, results not as expected

  28. 28

    Assigning randint values to objects [Python]

  29. 29

    What causes these default array values?

HotTag

Archive