Python Concepts/With Statement
Contents
Objective[edit]

Lesson[edit]
The With Statement[edit]The To use the with open("hello.txt", "r") as file:
print(file.read())
Hello, world!
try:
file = open("hello.txt", "r")
print(file.read())
except IOError:
print("An I/O error has occurred!")
except:
print("An unknown error has occurred!")
finally:
file.close()
Hello, world!

Examples[edit]
When using Python's decimal module[edit]Normal exit[edit]This example shows how context is restored after exiting the 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]This example shows that context is restored after exiting the error = ''
try:
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)
except:
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]

Further Reading or Review[edit]

References[edit]
1. Python's documentation:
"decimal.localcontext", "decimal.Context"
