Python Concepts/With Statement

From Wikiversity
Jump to navigation Jump to search

Objective[edit | edit source]

Books-aj.svg aj ashton 01f.png
  • Learn about the with statement, its purpose, and its relationship with the try statement.
  • Learn how and when to use the with statement.
  • Learn how to create objects that can be used with the with statement.

Lesson[edit | edit source]

Note: This lesson is diving pretty deep into the world of Python and it's recommended that you've finished both Definitions and Object-Oriented Programming on this course. If you haven't, you can skip to the next lesson.

The With Statement[edit | edit source]

The with statement is an error handling statement, similar to the try statement. The with statement is used to cleanup objects automatically. This helps reduce the amount of code that needs to be written, since the finally statement and the object's manually written cleanup is omitted.

To use the with statement, you'll need to specify an object you want to use followed by the as statement, which then ends with the variable name of that object. Here is an example using the with statement, assuming that the file hello.txt has the sentence Hello, world! in it.

with open("hello.txt", "r") as file:
Hello, world!

An example using the try, except, and finally statements. Again, it assumes that the file hello.txt has the sentence Hello, world! in it.

    file = open("hello.txt", "r")
except IOError:
    print("An I/O error has occurred!")
    print("An unknown error has occurred!")
Hello, world!

However, if an error occurs while the file is open, the file will not be closed. Previously, one would have to write a try...finally block to ensure a proper cleanup of objects, which would require a lot more work and sacrifice readability.

Examples[edit | edit source]

When using Python's decimal module[edit | edit source]

Normal exit[edit | edit source]

This example shows how context is restored after exiting the with statement in operations using Python's decimal module.

from decimal import *

import sys

print ('1. current precision =', getcontext().prec)

with localcontext(Context(prec=55)) as ctx:
    s = ctx.sqrt(Decimal(10)) # This uses ctx.prec
    print ('    2a. s =', s)
    s = Decimal(10).sqrt() # This uses getcontext().prec
    print ('    2b. s =', s)
    print ('    2c. ctx.prec =',ctx.prec)
    print ('    2d. current precision =', getcontext().prec)

s = +s
print ('3. s =',s)
print ('4. current precision =', getcontext().prec)
print ('5. ctx.prec =',ctx.prec)
1. current precision = 28
    2a. s = 3.162277660168379331998893544432718533719555139325216827
    2b. s = 3.162277660168379331998893544432718533719555139325216827
    2c. ctx.prec = 55
    2d. current precision = 55
3. s = 3.162277660168379331998893544
4. current precision = 28 # Restored.
5. ctx.prec = 55 # Unchanged.

Exit after exception[edit | edit source]

This example shows that context is restored after exiting the with statement because of an exception.

error = ''
  with localcontext() as ctx:
    print ('    6a. getcontext().prec =', getcontext().prec)
    ctx.prec = 55
    print ('    6b. getcontext().prec =', getcontext().prec)
    s = ctx.sqrt(Decimal(10))
    print ('    6c. s =',s)
    s = Decimal(10).sqrt()
    print ('    6d. s =',s)
    s = Decimal(-10).sqrt()
    print ('    6e. s =',s)
  error = sys.exc_info()

print ('7. error =', error)
print ('8. precision =', getcontext().prec)
    6a. getcontext().prec = 28
    6b. getcontext().prec = 55 # Same as context ctx.
    6c. s = 3.162277660168379331998893544432718533719555139325216827
    6d. s = 3.162277660168379331998893544432718533719555139325216827
7. error = (<class 'decimal.InvalidOperation'>, 
            InvalidOperation([<class 'decimal.InvalidOperation'>],), 
            <traceback object at 0x1018c4f88>)
8. precision = 28 # Restored.

Assignments[edit | edit source]

Crystal Clear app kedit.svg

Further Reading or Review[edit | edit source]

Tulliana launch.png Completion status: this resource is just getting off the ground. Please feel welcome to help!

References[edit | edit source]

1. Python's documentation:


2. Python's methods:

"decimal.localcontext", "decimal.Context"

3. Python's built-in functions: