Is there a better way than using globals to get interesting values from a context manager?
@contextmanager
def transaction():
global successCount
global errorCount
try:
yield
except:
storage.store.rollback()
errorCount += 1
else:
storage.store.commit()
successCount += 1
Other possibilities:
sort of globals...
makes the function more specific to a problem /less reusable
same problems as the tuple, but more legible
really bad idea
See http://docs.python.org/reference/datamodel.html#context-managers
Create a class which holds the success and error counts, and which implements the __enter__
and __exit__
methods.