This is a common pattern. Please consider using a python context manager to abstract the edit layer operations (startEditing...endEditing, etc.).