0

Some python class inheritance is having some unanticipated consequences, namely "sticky" attributes that I would have expected to be reset.

A simplified scenario, that still demonstrates what's happening:

class Foo(object):

    def __init__(self, headers={}):
        self.headers = headers

class Bar(Foo):

    def __init__(self, headers={}):
        super().__init__(headers=headers)

```

If I instantiate an instance of Bar, I can see .headers are blank. Then edit bar.headers, and see they are modified. But then instantiate another instance of Bar, and unexpectedly the .headers is still modified:

In [39]: bar = Bar()

In [40]: bar.headers
Out[40]: {}

In [41]: bar.headers['baz'] = True

In [44]: goober = Bar()

In [45]: goober.headers
Out[45]: {'baz': True}

Is there a better pattern for this kind of inheritance? Otherwise, everything else is working nicely and as expected.

martineau
  • 119,623
  • 25
  • 170
  • 301
ghukill
  • 1,136
  • 17
  • 42

0 Answers0