I'm receiving an error of AttributeError: __exit__ in the code below after finishing the with section. The Element object has is being returned in the with and it has __exit__ defined, so I'm baffled.
class Builder:
def __init__(self):
print("Builder init fires")
def __getattr__(self, name):
return Element(name, self)
class Element:
def __init__(self, name, builder):
self.name = name
self.builder = builder
print("Element init fires for name of", self.name)
def __call__(*args, **kargs):
print("CALL fires, now with attributes listed:")
for attr, value in sorted(kargs.items()):
print(' %s=>%s' % (attr, value))
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
aa = Builder()
with aa.feed(xmlns='http://www.w3.org/2005/Atom'):
print("INSIDE THE WITH")
__enter__, because your__call_returns None.AttributeErrorfor__exit__but under python 3.6.1 its for__enter__. I see you're correct that__call__()determines what is being returned by thewithand it needs to havereturn self. Thanks.with aa.feed: