👋 Hey there, I am Waylon Walker
I am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
Great use case for a context manager. Outside of opening files context managers are far too underutilized.
There is also an alternative class based syntax that you may run into on occasion.
classset_directory(object):"""Sets the cwd within the context
Args:
path (Path): The path to the cwd
"""def__init__(self,path:Path):self.path=pathself.origin=Path().absolute()def__enter__(self):os.chdir(self.path)def__exit__(self):os.chdir(self.origin)
Great use case for a context manager. Outside of opening files context managers are far too underutilized.
There is also an alternative
class
based syntax that you may run into on occasion.Great point! Thank you for providing a full class example. I'm sure people will find it useful to reference!