Home > Back-end >  Python Dictionary modification from different module
Python Dictionary modification from different module

Time:09-03

I am using a python dictionary from a module:

custom_dictionary.py

sas = {"apple":505, "mango":404}

my test code looks like this which modifies the dictionary from the imported module.

test.py:

# modifying dictionary, in turn it modifies original data 
from custom_dictionary import sas

current_org = 5
my_org = 0

def modify_dictionary(org):
    sas["apple"] = org
    sas["mango"] = org 1

if current_org != my_org:
   modify_dictionary(current_org)

Is this the right way to modify the dictionary which is located in another module in python? Or this should be avoided and dictionary should be copied locally in the test.py file and then modified.

example: test.py:

  # modifying dictionary copy by creating a local dictionary
    from custom_dictionary import sas
    local_sas = sas
    current_org = 5
    my_org = 0
    
    def modify_dictionary(org):
        local_sas["apple"] = org
        local_sas["mango"] = org 1
    
    if current_org != my_org:
       modify_dictionary(current_org)

Another option that I have is to use global, but I am not sure which one would be the best approach

using global or

directly modifying it or

using a local dictionary and copy it and then modify it.

example with global:

# modifying dictionary, using **global**
from custom_dictionary import sas

current_org = 5
my_org = 0

def modify_dictionary(org):
    global sas
    sas["apple"] = org
    sas["mango"] = org 1

if current_org != my_org:
   modify_dictionary(current_org)

CodePudding user response:

You made a tiny mistake inside the condition, you wrote myorg instead of "my_org". About your question, it depends, if you want your original dictionary to be modified, then what you did here is right. Otherwise you have to create a variable which copies the original dictionary

CodePudding user response:

You can directly modify the imported dictionary variable in the scope of test.py file. Modified sas variable does not modify your original dictionary variable from the custom_dictionary.py file. As the imported variable scope is limited to current module(in this case it's test.py) only.

Also with local_sas = sas you are not actually copying the object. Here you are just creating the extra object reference to the object sas. now you will have two object references (local_sas and sas) pointing to only one dictionary object in memory. if you want to copy the object in python, you can use copy module. In your case specifically, copy.deepcopy() method can be used.

CodePudding user response:

What is your desired behaviour? All your examples are logically equivalent.

Suppose you have another Python file test2.py, and you do:

import test
from custom_dictionary import sas
print(sas['apple'])  # Should this print 505 or 5?

If you want to change only your local copy, just assignment is not enough. You'll have to make a copy like so: local_sas = sas.copy().

Note that you don't need to use global unless you want to rebind the sas variable itself.

I recommend learning about names and values, it'll help explain the difference between assignment and mutation, and make it obvious both when you need to make a copy of something and what operations actually make a copy.

  • Related