(Python)Object dict copy gets modified for no apparent reason - python

class Object:
def __init__(self,dict):
self.dict = dict
a = Object({1:"hello",2:"lol"})
b = Object(a.dict)
b.dict.pop(1) #remove the element with key 1
print(a.dict, b.dict)
>>{2: 'lol'} {2: 'lol'}
for some reason the "a" object's dictionary gets modified too.
I've tried the same thing with a different attribute, like an int variable, and the problem didn't happen. I really don't know what to do :(

you are not copying the dictionary you are just pointing both a and b to the same dict, thats why its changing both.
you can use dict.copy() to create a shallow copy.
b = Object(a.dict.copy())
this will copy the dict but not any nested dictionaries, for that you need a deep copy.
import copy
b = copy.deepcopy(a.dict)

Related

Passing dictionary as an input parameter to a function global/local variable issue

Hi can anyone clarify this issue for me. Here is a sample of the code:
def test1(d1):
d1_adj = d1.copy()
d1_adj.get(0)[0] *= 10
return d1_adj
d1 = {0: [10, 10]}
d1_adj = test1(d1)
print d1
{0: [100, 10]}
Why does d1 dictionary get updated and how can I come across this issue if I want to preserve that values of the original input dictionary and only update the values of the one that has been copied
Thanx
It is because d.copy() is shallow copy. You need deep copy to accomplish this.
from copy import deepcopy
Then
d1_adj = deepcopy(d1)
copy does a shallow copy, and not a deep copy, as you intended to do. Consequently, it does not create a copy of the entire list at key 0, and modifies the original list.
You may perform a manual deep copy of the values within the dictionary by traversing it once. Or better still, unless there are any requirements against using it, you may utilize copy module to perform deep copy(i.e. copy.deepcopy) operation. All you need to do is to update your test1 function as follows.
from copy import deepcopy
def test1(d1):
d1_adj = deepcopy(d1)
d1_adj.get(0)[0] *= 10
return d1_adj

Scope of dynamic view objects in Python 3.x

What happens to a view object in python after the original object have been deleted? For example,
a = {'foo': 1 , 'bar': 2, 'baz': 3 }
b = a.keys() # => dict_keys(['bar', 'foo', 'baz'])
At this point, if any changes are made to the dictionary, they are reflected in b. For example,
a['qux'] = 4
print(b) # => dict_keys(['bar', 'qux', 'foo', 'baz'])
However, when the dictionary is deleted, the dynamic variable still contains all the value of the keys from the deleted dictionary.
del a
a # NameError: name 'a' is not defined
print(b) # => dict_keys(['bar', 'qux', 'foo', 'baz'])
Question
In essence, I want to know if I have to always make sure I delete any variables with the keys values even after the dictionary is deleted. Can this will be a potential cause for a memory leak if the dictionary is large?
Any feedback will be appreciated.
Side Note:
And, yes I know I can put the keys in a list:
c = list(a.keys())
but I am using view objects because of their smaller memory footprint in comparison to list.
I would recommend not to use the term variable. Better use name that allows access to an object. For your example a and b are names for the objects dictionary and the keys of a dictionary. In Python 3 dict.keys() gives you a key-view object that reflects the changes in the underlying dictionary. Therefore, the key-view object keeps a reference to the dictionary.
So you don't delete the dictionary but rather the name pointing to it. Only if there are no more names (references to the dictionary) left, will the garbage collector remove the dictionary.
If you program in a structured way with functions that do not work on global objects, memory leaks are rather rare. In practice del is typically used sparingly.
del only decreases reference counter to an object in python's underhood. Dictionary itself will be deallocated from memory (disposed) far later, when no references left active in program.
When you save keys() product in a variable, you increase dict's reference counter, additionaly to dict's variable itself. Variable will not be deallocated while any reference left to it.
I can express your code in very simple, but absolutely same construction:
a = dict()
b = a # reference only
del(b)
# dict is not garbage-collected, as a still holds the reference
a != None # True

It's possibile to use set function on a object basing only one attribute?

I'm creating this type of object:
class start_url_mod ():
link = ""
id = 0
data = ""
I'm creating a list of this object and I want to know if there is some way in order to delete one of then if I find same link attribute.
I know the function set() for the deleting of duplicates in a "sample" list, but there is something very fast and computational acceptable?
Use a dict key-ed on the attribute. You can preserve order with collections.OrderedDict:
from collections import OrderedDict
# Keep the last copy with a given link
kept_last = OrderedDict((x.link, x) for x in nonuniquelist).values()
# Keep the first copy with a given link (still preserving input order)
kept_first = list(reversed(OrderedDict((x.link, x) for x in reversed(nonuniquelist)).viewvalues()))
If order is not important, plain dict via dict comprehensions is significantly faster in Python 2.7 (because OrderedDict is implemented in Python, not C, and because dict comprehensions are optimized more than constructor calls; in Python 3.5 it's implemented in C):
# Keep the last copy with a given link but order not preserved in result
kept_last = {x.link: x for x in nonuniquelist}.values()
# Keep the first copy with a given link but order not preserved in result
kept_first = {x.link: x for x in reversed(nonuniquelist)}.values()
You can use a dictionary with the attribute that you're interested in being the key ...

Why don't Python dictionaries treat keys independently in this script? [duplicate]

This question already has answers here:
How to copy a dictionary and only edit the copy
(23 answers)
Closed 8 years ago.
I'm expecting my frustration to be overridden with some enlightenment - here's a minimal version of the script to demonstrate the problem:
First I create a dictionary:
dic = {
'foo':{},
'bar':{}
}
Then we instantiate a template dictionary that can be iteratively appended
to keys of dic:
appendic= {
'is':'', # '' is a terminal value to be replaced later
}
So here we append appendic to each of the keys in dic:
dic['foo'] = appendic
dic['bar'] = appendic
Now we replace the terminal values, '', with something meaningful:
dic['foo']['is'] = 'foo'
dic['bar']['is'] = 'bar'
At this point, my intuition tells me that if we call:
print(dic['foo']['is']) we get 'foo'
But instead Python returns 'bar' ... to my un-trained mind that is counter-intuitive.
Questions:
How can I tell Python to keep the keys of dic independent?
Why is this the default behaviour? What use cases does this have?
When you assign a appendic to two different keys, Python doesn't make a copy. It assigns a reference instead.
As a result, both dic['please_make_me_Foo'] and dic['dont_make_him_Bar'] refer to the same object. These are not separate dictionaries, they are both the same object, the one appendic also references to.
If you expected these to be separate dictionaries, create a copy of appendic instead. The dict.copy() method creates a shallow copy of a dictionary:
dic['please_make_me_Foo']= appendic.copy()
dic['dont_make_him_Bar'] = appendic.copy()
Shallow means that a new dictionary is created and all references to keys and values contained are copied over.
If appendic itself contains values that are also dictionaries, these would not be copied. The new copy and appendic would both refer to the same values. In most cases, that's not a problem because most primitive values (strings, integers, etc.) are immutable, and you never notice references are shared as you replace such values with new ones.
You make a dict:
appendic= {
'Python_made_me':''
}
Add it to your other dict twice
dic['please_make_me_Foo']= appendic
dic['dont_make_him_Bar'] = appendic
And set the single dict's Python_made_me value twice
dic['please_make_me_Foo']['Python_made_me'] = 'Foo'
dic['dont_make_him_Bar']['Python_made_me'] = 'Bar'
But because they're the same dict, the second line overwrites the first
If you need to copy it, you need to use the copy method:
dic['please_make_me_Foo']= appendic.copy()
dic['dont_make_him_Bar'] = appendic.copy()
ok, I'm just going to write this as a complement to the other answers. When you manipulate a dictionary, you manipulate the reference to an instance, which is the root cause of your mistake. Using hex(id(foo)) you get the memory address of foo, so let's show the address of d instance in the following example to make that tangible:
>>> hex(id(d))
'0x10bd95e60'
>>> hex(id(e[1]))
'0x10bd95e60'
>>> hex(id(f[1]))
'0x10bd95e60'
so if you add or remove values from e[1], you're actually changing the same instance as the one pointed by d, and as a dictionary is mutable, i.e. you can change values within.
Now you're wondering why that does not happen when you're handling integers? Because, in fact it does, it's just that integers are not mutable:
>>> i = 1
>>> hex(id(i))
'0x10ba51e90'
>>> j = i
>>> hex(id(j))
'0x10ba51e90'
>>> i = 2
>>> hex(id(i))
'0x10ba51eb0'
i.e. i is pointing to another place in the memory.
It's possible to create a mutable integer though, by using a class:
>>> class Integer:
... def __init__(self, i):
... self.i = i
...
>>> i = Integer(2)
>>> hex(id(i))
'0x10bd9b410'
>>> j = i
>>> hex(id(j))
'0x10bd9b410'
>>> j.i = 2
>>> i.i
2
>>> hex(id(i))
'0x10bd9b410'
In order to create a new instance of the same dictionary, you need to use the copy() member of a dict:
>>> hex(id(d))
'0x10bd95e60'
>>> w = d.copy()
>>> x = d.copy()
>>> y = d.copy()
>>> hex(id(w))
'0x10bd96128'
>>> hex(id(x))
'0x10bd95f80'
>>> hex(id(y))
'0x10bd96098'
dic['please_make_me_Foo']= appendic
dic['dont_make_him_Bar'] = appendic
appendic is an object - you are assigning a reference to the same object to both keys in dic. So when you change one, you change both.
Try this instead:
dic['please_make_me_Foo']= appendic.copy()
dic['dont_make_him_Bar'] = appendic.copy()

why does updating a dict that was appended to a list change the list?

My code will be more clear I think-
someList = list()
foo = {'a':'b'}
someList.append(foo)
print someList
>>> [{'a':'b'}]
defaultbazz = {'a':2, 'b':'t', 'c':'gg'}
for k, v in defaultbazz.iteritems():
foo[k] = v
print someList
>>> [{'a': 2, 'c': 'gg', 'b': 't'}]
Shouldn't the last print be [{'a':'b'}]? I didn't updated the someList, I want it as is..
It's seems to me uninterpreted behavior..
But if that's how python works, how can I find workaround? Even setting a new dict updates the original one dict.. I mean:
someList = list()
foo = {'a':'b'}
someList.append(foo)
print someList
>>> [{'a':'b'}]
bar = foo
defaultbazz = {'a':2, 'b':'t', 'c':'gg'}
for k, v in defaultbazz.iteritems():
bar[k] = v
print someList
>>> [{'a': 2, 'c': 'gg', 'b': 't'}]
I'll be thankful if someone can maybe explain me why it's happen..
It looks like you are expecting your dict to be copied when you add it to a list or assign it to a new variable, but that is not how Python operates. If you assign a dict -- actually, if you assign any object -- you are not creating a new object, but instead you are simply giving your object a new name. (An object can have multiple names.)
So, when you edit your object under the new name, the single instance of that object changes, and that change is visible when you access the object through any name.
If you want to copy your object, then you can do this:
bar = dict(foo)
or
bar = foo.copy()
To simplify:
a = {2: 3}
b = [a]
b contains a "reference" to a (and is a dict which is mutable) - so if a is modified then accessing a via the list b, will display the modified a.
You have to explicitly create a copy of a, which can be done in this case as:
b = [dict(a)]
But you should look at the copy module for copy.copy() and copy.deepcopy()
Dictionaries are mutable objects, hence the result of your script.
I guess you want a new object, i.e. a copy of the original one:
import copy
someList.append(copy.copy(foo))
Variables in Python are just names of objects. If you change the object from any name "attached" to it, you will see the changes from every other name. Python never creates copies automatically for you, in particular:
someList.append(foo)
doesn't create a copy of foo and put it on someList, it appends the object that the name foo refers to onto the list.
You can create a second name for this object
bar = foo
but this does not create a copy either. In particular
foo['x'] = 42
and
bar['x'] = 42
will then operate on exactly the same object. You can verify this by printing the memory address of the object:
print id(foo), id(bar)
and see that they are the same.
If you need a copy in Python, you'll need to create one explicitly. Depending on what you need, the copy module -- either copy.copy() or copy.deepcopy() -- will do what you want:
import copy
bar = copy.copy(foo)
print id(foo), id(bar)
should now print different memory locations.
Dicts are mutable, which means that they can change. It's because foo is inside of someList and you're changing foo in the for-loop. Take a look at this simple example:
a_dict = {'a':'b'}
a_list = [a_dict]
print a_list # [{'a':'b'}]
#change the dict
a_dict['a'] = 'c'
print a_list # [{'a':'c'}]

Categories