Closed Benjamin-Loison closed 1 month ago
class PrintBeginAndEnd:
def __init__(self, sectionLabel):
self.sectionLabel = sectionLabel
def __enter__(self):
self.printState('begin')
def __exit__(self, _exc_type, _exc_value, _traceback):
self.printState('end')
def printState(self, state):
print(f'{self.sectionLabel}: {state}')
myVariable = 42
with PrintBeginAndEnd('Print `myVariable`'):
print(f'{myVariable=}')
Print `myVariable`: begin
myVariable=42
Print `myVariable`: end
Modified from https://medium.com/@badu_bizzle/custom-contexts-in-python-24e4525182eb.
Preferably would like a scheme like
with
:So without declaring a function, so something like: