1

I have a weird behaviour of Python that is probably pretty rational, but I don't understand it. look at this code here:

def changeDict(status):
    hotstatus = status
    for e in hotstatus:
        if hotstatus[e]:
            hotstatus[e] = "Successful"
        else:
            hotstatus[e] = "Failed"
    print(hotstatus)
    message = f"""
    Operation start: {hotstatus["operation-start"]}
    Operation stop: {hotstatus["operation-stop"]}
    """
    return message

def initalDict():
    status = {}
    status["operation-start"] = True
    status["operation-stop"] = False
    print(status)
    message = changeDict(status)
    print(message)
    print(f"This is the status: {status}")

initalDict()

How does this output come from this:

{'operation-start': True, 'operation-stop': False}
{'operation-start': 'Successful', 'operation-stop': 'Failed'}

    Operation start: Successful
    Operation stop: Failed
    
This is the status: {'operation-start': 'Successful', 'operation-stop': 'Failed'}

Shouldn't the last line still read True and False as the values? I am very confused. It would be great if someone could help me with that mystery :D.

Kind regards

schickf
  • 33
  • 2
  • `if hotstatus[e]` checks whether the value is `True` (or `False`). However, when `True`, `changeDict` replaces the `True` with `'Successful'` (and the `False` with `'Failed'`) – inspectorG4dget Jul 05 '21 at 13:58

4 Answers4

1

In Python dict is mutable, so when you assign one to another variable:

 hotstatus = status  

that simply creates an alias for the underlying, mutable dict. Modifying the dict aliased to by hotstatus, modifies the same dict refenced by the variable name status.

If you try this instead:

   import copy
   ... code ...
   hotstatus = copy.deepcopy(status)  

you'll make a copy of the dict that status refers to (actually a deepcopy, everything will be copied so even if there's another mutable stored in that dict all of its contents will be copied over too) so hotstatus has its very own dict to work with. Modifications to hotstatus will have no affect on what status refers to.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

When you send the dictionary status to the function changeDict() you are sending the reference to the status dictionary and not a copy of it.

So changing the values of hotstatus in changeDict() will change the status dictionary as both are referring to the same object.

You can use hotstatus = status.copy() to use copy of status dict.

This will give you a clear idea behind this concept.

Ram
  • 4,724
  • 2
  • 14
  • 22
0

I see you are using hotstatus = status. which makes both of them point to same memory location. so both of them will be updated when you update one dictionary.

refer this link for more info : here

0

When you do hotstatus = status it doesn't create a copy of your dictionary input you are just changing the name, if you want to create a copy you should use hotstatus = status.copy(), then you will be able to edit hotstatus without changing status