Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: use keyword with for the first time solution in Uncategorized category for I Love Python! by hbczmxy
"""
Sharing this weird solution, really because I came across a "Share solution with something new" task that asked me to use "with" and share the new solution
"""
class Sample:
def __init__(self, msg = "I love Python!"):
self.msg = "I love Python!"
def __enter__(self):
print("I'm now in the context of Python! It's in __enter__")
return self
def i_love_python(self):
return self.msg
def __exit__(self, exc_type, exc_val, exc_tb):
print("I'm now out of the context of Python! It's in __exit__")
def get_sample():
return Sample()
with get_sample() as sample:
i_love_python = sample.i_love_python
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert i_love_python() == "I love Python!"
Feb. 25, 2022
Comments: