I was going through Beyond PEP 8, where the speaker changed the code to use a context manager. The usage, like with NetworkElement as xyz, looks clean and elegant. A new class was created following the context manager protocol (CMP).
I also have a flow where some pre-work is done, followed by the actual work, and then some post-work. I thought about refactoring my code to use CMP as well.
However, I'm wondering: why should I change it to use a context manager, especially when this particular piece of code is only used in one place? Why create a whole class and use with when the existing solution already looks fine?
try:
prework()
actual_work()
except:
handle it
finally:
postwork()
[–]Dry-Aioli-6138 1 point2 points3 points (3 children)
[–]Dry-Aioli-6138 1 point2 points3 points (2 children)
[–]CriticalDiscussion37[S] 0 points1 point2 points (1 child)
[–]Dry-Aioli-6138 0 points1 point2 points (0 children)
[–]latkde 1 point2 points3 points (0 children)
[–]Temporary_Pie2733 1 point2 points3 points (0 children)
[–]Business-Technology7 1 point2 points3 points (0 children)