Unexpected behavior using contextmanager on a class method
Peter Otten
__peter__ at web.de
Tue Aug 7 12:04:29 EDT 2012
Thomas Draper wrote:
> I want to use with..as in a "reversible circuit generator". However, it
> seems that @contextmanager changes the expected nature of the class. I
> tried to distill the problem down to a simple example.
>
> import contextlib
>
> class SymList:
The problem you experience has nothing to do with context managers, you have
a mutable default argument in your __init__().
> def __init__(self, L=[]):
L is initialised with an empty list exactly once, when the method is
defined; any changes you make to the list will be seen by all instances that
use the default. The fix is
def __init__(self, L=None):
if L is None:
L = []
> self.L = L
>
> @contextlib.contextmanager
> def SymAdd(self, a):
> self.L.append(a)
> yield
> self.L.append(a)
>
> SL = SymList()
> with SL.SymAdd(3):
> SL.L.append(5)
> print(SL.L) # Expect and see [3, 5, 3]
> SL2 = SymList() # New object. Should have default values.
> print(SL2.L) # Expect [] and see [3, 5, 3]
>
> Why is the data member SL2.L referring to the data member SL.L? Has the
> @contextmanager somehow made all instantions of the class related?
More information about the Python-list
mailing list