Bird
Raised Fist0
Pythonprogramming~20 mins

With statement execution flow in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
With Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this with statement?
Consider this Python code using a custom context manager class. What will it print when run?
Python
class MyContext:
    def __enter__(self):
        print('Enter')
        return 'resource'
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Exit')

with MyContext() as res:
    print('Inside with:', res)
AEnter\nInside with: resource\nExit
BInside with: resource\nEnter\nExit
CEnter\nExit\nInside with: resource
DExit\nEnter\nInside with: resource
Attempts:
2 left
💡 Hint
Remember, __enter__ runs before the block, __exit__ runs after.
Predict Output
intermediate
2:00remaining
What happens if an exception occurs inside the with block?
Look at this code. What will be printed when the exception is raised inside the with block?
Python
class CM:
    def __enter__(self):
        print('Start')
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('End')
        print('Exception:', exc_type.__name__ if exc_type else 'None')

with CM():
    print('Inside')
    raise ValueError('Oops')
AStart\nInside\nException: ValueError\nEnd
BStart\nEnd\nInside\nException: ValueError
CStart\nInside\nEnd\nException: ValueError
DStart\nInside\nEnd\nException: None
Attempts:
2 left
💡 Hint
The __exit__ method receives exception info if an error happens.
Predict Output
advanced
2:00remaining
What is the output when __exit__ suppresses the exception?
This context manager suppresses exceptions by returning True from __exit__. What will be printed?
Python
class SuppressError:
    def __enter__(self):
        print('Begin')
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Cleanup')
        return True

with SuppressError():
    print('Running')
    raise RuntimeError('Fail')
print('After with')
ABegin\nRunning\nCleanup\nAfter with
BBegin\nRunning\nAfter with
CBegin\nCleanup\nRunning\nAfter with
DBegin\nRunning\nCleanup
Attempts:
2 left
💡 Hint
Returning True from __exit__ stops the exception from propagating.
Predict Output
advanced
2:00remaining
What is the output of nested with statements with context managers?
Given these two context managers used in nested with statements, what will be printed?
Python
class CM1:
    def __enter__(self):
        print('Enter CM1')
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Exit CM1')

class CM2:
    def __enter__(self):
        print('Enter CM2')
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Exit CM2')

with CM1():
    with CM2():
        print('Inside both')
AEnter CM2\nEnter CM1\nInside both\nExit CM1\nExit CM2
BEnter CM1\nEnter CM2\nInside both\nExit CM2\nExit CM1
CEnter CM1\nEnter CM2\nExit CM2\nInside both\nExit CM1
DEnter CM2\nEnter CM1\nExit CM1\nInside both\nExit CM2
Attempts:
2 left
💡 Hint
The inner with runs fully before the outer with exits.
🧠 Conceptual
expert
3:00remaining
What is the value of variable 'result' after this with statement?
Analyze this code. What will be the value of 'result' after the with block finishes?
Python
class CM:
    def __enter__(self):
        return 10
    def __exit__(self, exc_type, exc_val, exc_tb):
        return True

result = None
with CM() as val:
    result = val + 5
    raise Exception('Error')
print('Done')
A10
BNone
CException is raised, so no value assigned
D15
Attempts:
2 left
💡 Hint
Even if an exception is raised, if __exit__ returns True, the block completes and variables keep their values.

Practice

(1/5)
1. What does the with statement do in Python?
easy
A. It defines a function inside another function.
B. It automatically manages setup and cleanup actions.
C. It creates a new thread for parallel execution.
D. It repeats a block of code multiple times.

Solution

  1. Step 1: Understand the purpose of the with statement

    The with statement is designed to manage resources by automatically handling setup and cleanup.
  2. Step 2: Compare options with this behavior

    Only It automatically manages setup and cleanup actions. describes automatic setup and cleanup, which matches the with statement's role.
  3. Final Answer:

    It automatically manages setup and cleanup actions. -> Option B
  4. Quick Check:

    with manages resources = C [OK]
Hint: Remember: with = automatic setup and cleanup [OK]
Common Mistakes:
  • Confusing with loops or function definitions
  • Thinking with creates threads
  • Assuming with repeats code
2. Which of the following is the correct syntax to use a with statement for opening a file named 'data.txt'?
easy
A. with open('data.txt') as file:
B. with open('data.txt') file:
C. with open('data.txt') -> file:
D. with open('data.txt') = file:

Solution

  1. Step 1: Recall the correct with syntax

    The correct syntax is: with expression as variable:
  2. Step 2: Match the syntax with options

    with open('data.txt') as file: matches the correct syntax exactly. Others use invalid symbols or miss the 'as' keyword.
  3. Final Answer:

    with open('data.txt') as file: -> Option A
  4. Quick Check:

    Correct with syntax uses 'as' = A [OK]
Hint: Use 'with ... as ...:' syntax for resource management [OK]
Common Mistakes:
  • Omitting 'as' keyword
  • Using '=' or '->' instead of 'as'
  • Missing colon at the end
3. What will be the output of this code?
class Resource:
    def __enter__(self):
        print('Enter')
        return 'resource'
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Exit')

with Resource() as r:
    print('Using', r)
medium
A. Using resource\nEnter\nExit
B. Using resource\nExit\nEnter
C. Enter\nExit\nUsing resource
D. Enter\nUsing resource\nExit

Solution

  1. Step 1: Understand the order of with execution

    The __enter__ method runs first, printing 'Enter'. Then the block runs, printing 'Using resource'. Finally, __exit__ runs, printing 'Exit'.
  2. Step 2: Match output order with options

    Enter\nUsing resource\nExit matches the sequence: Enter, Using resource, Exit.
  3. Final Answer:

    Enter Using resource Exit -> Option D
  4. Quick Check:

    __enter__ -> block -> __exit__ = B [OK]
Hint: Remember: enter prints first, then block, then exit prints [OK]
Common Mistakes:
  • Assuming block runs before __enter__
  • Mixing order of print statements
  • Ignoring __exit__ call after block
4. What is wrong with this code?
class MyContext:
    def __enter__(self):
        print('Start')
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('End')

with MyContext() as ctx:
    print('Inside')
medium
A. The __enter__ method must return a value.
B. The __exit__ method is missing parameters.
C. The with statement is missing a colon.
D. The class must inherit from a base context class.

Solution

  1. Step 1: Check the __enter__ method requirements

    The __enter__ method should return a value that is assigned to the variable after 'as'. Here, it returns nothing (None).
  2. Step 2: Identify the error caused by missing return

    Because __enter__ returns None, ctx becomes None, which is allowed but often unintended. The code runs but usually __enter__ should return a useful value.
  3. Final Answer:

    The __enter__ method must return a value. -> Option A
  4. Quick Check:

    __enter__ must return for 'as' variable = D [OK]
Hint: Always return a value from __enter__ if using 'as' [OK]
Common Mistakes:
  • Not returning anything from __enter__
  • Thinking __exit__ parameters are optional
  • Forgetting colon after with statement
5. You want to write a context manager that counts how many times the block inside with runs and prints the count after all uses. Which approach correctly implements this behavior?
hard
A. Use a class with instance variable counting and print in __enter__.
B. Use a function with yield and print count after the yield.
C. Use a class with a class variable to count entries and print in __exit__.
D. Use a function that returns a list of counts each time it's called.

Solution

  1. Step 1: Understand the requirement to count multiple uses

    Counting how many times the block runs requires storing count across instances, so a class variable is needed.
  2. Step 2: Identify where to print the count

    Printing after all uses means printing in __exit__ after each block ends. Using a class variable allows accumulation.
  3. Step 3: Evaluate options

    Use a class with a class variable to count entries and print in __exit__. uses a class variable and prints in __exit__, matching the requirement. Use a function with yield and print count after the yield. is a generator but doesn't accumulate count across uses. Use a class with instance variable counting and print in __enter__. uses instance variable, which resets each time. Use a function that returns a list of counts each time it's called. is unrelated to context managers.
  4. Final Answer:

    Use a class with a class variable to count entries and print in __exit__. -> Option C
  5. Quick Check:

    Class variable + __exit__ print = A [OK]
Hint: Use class variable to track count across with blocks [OK]
Common Mistakes:
  • Using instance variables that reset each time
  • Printing count too early in __enter__
  • Confusing generator functions with context managers