From b0a1db1eed33549c5c25eef1034bc4878dc21993 Mon Sep 17 00:00:00 2001 From: neingeist Date: Wed, 3 Sep 2014 19:21:04 +0200 Subject: [PATCH] play around with pickle --- mixed_pickles.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 mixed_pickles.py diff --git a/mixed_pickles.py b/mixed_pickles.py new file mode 100644 index 0000000..c039eed --- /dev/null +++ b/mixed_pickles.py @@ -0,0 +1,30 @@ +from __future__ import division, print_function + +import cPickle as pickle + + +class SomeObject(object): + def __eq__(self, other): + """Default __eq__ would always return False.""" + return True + + pass + +a = (1, 2) +b = "zwei" +c = [3, 4, 5, 6] +d = c +e = SomeObject() +f = e +before_pickle = [a, b, c, d, e, f] + +after_pickle = pickle.loads(pickle.dumps(before_pickle)) + +# should get equal objects, but not the same: +for i in range(6): + assert before_pickle[i] == after_pickle[i] + assert id(before_pickle[i]) != id(after_pickle[i]) + +# same objects stay the same: +assert id(after_pickle[2]) == id(after_pickle[3]) +assert id(after_pickle[4]) == id(after_pickle[5])