-2

I have a class in python that is used to generate parameter files for a piece of software. This software is used in an iterative process and requires a new set of parameter files for each iteration. As such the class PropGen is called upon to create the new files just before each iteration.

The class is feed the default parameters for these files once before the entire process and then given the current iteration modifies these parameters and writes them to the new file. The way I have been accomplishing this is by storing the defaults into an OrderedDict self.params and creating another OrderedDict self.output_params that collects the modified values before being used to write to a file.

My problem is that no matter how I move the values from self.params to self.output_params the two dictionaries have the same object id and thus any changes to self.output_params are reflected in self.params. So far I have tried the following:

EDIT Found error with missing call to deepcopy at end of file.

class A(object):
    def __init__(self):
        self.a = OrderedDict({'a':1, 'b':2})
        self.b = deepcopy(self.a)
        self.iter = 0

    def do_some_work(self, key):
        val = self.a[key]           
        self.b[key] = val.replace('#', self.iter)

    def create(self):
        lines = []
        for item in self.output_params.items():
            lines.append('='.join(item) + '\n')
        with open(filename, 'w') as file_obj:
            file_obj.writelines(lines)
        # Here was the error
        self.b = self.a
        # should have been self.b = deepcopy(self.a)
Grr
  • 15,553
  • 7
  • 65
  • 85
  • 2
    What makes you think they share the same object id? `==` does not check object identity, it checks *equality*. – BrenBarn Apr 19 '17 at 20:41
  • i checked with `id()` as well – Grr Apr 19 '17 at 20:43
  • 3
    Then please show a self-contained example demonstrating the actual problem. Nothing in your posted code actually shows that the objects have the same id, and it's impossible to test because your examples are not self-contained. – BrenBarn Apr 19 '17 at 20:47
  • Fair point. Will do – Grr Apr 19 '17 at 20:50

1 Answers1

0

The problem is in something you haven't shown us, so work harder at providing an executable example that actually demonstrates the problem. For example, you can run this:

from collections import OrderedDict

class C:
    def __init__(self):
        self.d1 = OrderedDict(a=1, b=2)

    def copy(self):
        self.d2 = self.d1.copy()

c = C()
c.copy()
print(c.d1 is c.d2)
c.d1['a'] = 666
print(c.d1)
print(c.d2)

For me, under Python 2 or 3, it prints:

False
OrderedDict([('a', 666), ('b', 2)])
OrderedDict([('a', 1), ('b', 2)])

What does it print for you? Assuming it works for you, what haven't you shown us about your code?

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • 2
    So in preparing as condensed an example as I could I found my error. I had omitted a deepcopy when returning self.b to the original state. – Grr Apr 19 '17 at 21:07