I am new to Python and am playing with Pickle and don't understand how this works
I define a defaultdict, write it to pickle. Then in a different script I read it and it still behaves like a defaultdict even without importing collections
script1:
import pickle
from collections import defaultdict
x = defaultdict(list)
x['a'].append(1)
print(x)
with open('pick','wb') as f:
pickle.dump( x, f )
script2:
import pickle
with open('pick','rb') as f:
x = pickle.load( f )
x['b'].append(2)
print(x)
y = dict()
try:
y['b'].append(2)
print(y)
except KeyError:
print("Can't append to y")
running:
$ python3 pick2.py
defaultdict(<class 'list'>, {'a': [1], 'b': [2]})
Can't append to y
So, the 2nd script doesn't import defaultdict but the pickled x still acts like one. I'm confused :)
How does this work in Python? Thanks for any info :)