Unexpected behavior using contextmanager on a class method
Thomas Draper
tgdrape at gmail.com
Tue Aug 7 11:30:15 EDT 2012
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:
def __init__(self, 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