Challenge: Implement a Basic Context Manager
You have learned about the context management protocol and how the with statement uses objects that implement __enter__ and __exit__ methods to manage resources. Now, put your understanding into practice by implementing a simple context manager class. Your class will simulate acquiring and releasing a resource by printing messages when entering and exiting the context block. This exercise reinforces the protocol and prepares you for more advanced custom context managers.
Swipe to start coding
Implement a class named SimpleResourceManager that acts as a basic context manager.
- The
__enter__method must print"Resource acquired."when the context is entered. - The
__exit__method must print"Resource released."when the context is exited.
Løsning
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 8.33
Challenge: Implement a Basic Context Manager
Sveip for å vise menyen
You have learned about the context management protocol and how the with statement uses objects that implement __enter__ and __exit__ methods to manage resources. Now, put your understanding into practice by implementing a simple context manager class. Your class will simulate acquiring and releasing a resource by printing messages when entering and exiting the context block. This exercise reinforces the protocol and prepares you for more advanced custom context managers.
Swipe to start coding
Implement a class named SimpleResourceManager that acts as a basic context manager.
- The
__enter__method must print"Resource acquired."when the context is entered. - The
__exit__method must print"Resource released."when the context is exited.
Løsning
Takk for tilbakemeldingene dine!
single