MCS 275 Spring 2023
Emily Dumas
Reminders and announcements:
What's the output?
A = 2
B = [3,4,5]
C = [5,6,7]
def f():
A = 7
B[0] = 7
C = [7,7,7]
f()
print(A)
print(B[0])
print(C[0])
What's the output?
A = 2
B = [3,4,5]
C = [5,6,7]
def f():
A = 7 # new local A
B[0] = 7 # B.__setitem__(0,7) on global B
C = [7,7,7] # new local C
f()
print(A) # 2 (unchanged)
print(B[0]) # 7 (same B, new item at index 0)
print(C[0]) # 5 (unchanged)
Text cells (Colab) or markdown cells (Jupyter) contain formatted text. When editing, formatting is specified with a language called Markdown.
# Heading level 1
## Heading level 2
### Heading level 3
* Bullet list item
* Another bullet list item
1. Numbered list item
1. Another numbered list item
Links: [text to display](https://example.com)
Acquire, use, and free a resource.
x = resource() # open file, connect to database, ...
x.action()
x.action2(y,z)
if x.status() == w:
...
x.close() # or "release" or "delete"
# ACQUIRE
fp = open("data.txt","w",encoding="UTF-8")
# USE
for s in L:
fp.write(s+"\n")
# RELEASE
fp.close()
Is the resource always freed? What if an exception is raised?
All files are closed when a program exits, but open files are a limited resource.
Will this function always close the file?
def file_contains_walrus(fn):
"""Return True if "walrus" is a line of file `fn`"""
fileobj = open(fn,"r",encoding="UTF-8")
for line in fileobj:
if line.strip() == "walrus":
fileobj.close()
return True
return False
Currently, in CPython (the usual interpreter): Yes.
In CPython, local variables are deleted as soon as a function returns. Deleting a file object closes the file.
But this isn't a language guarantee!
Use with
block to ensure automatic file closing.
with open("data.txt","w",encoding="UTF-8") as fileobj:
fileobj.write(...)
fileobj.write(...)
# other write operations...
print("At this point, the file is already closed")
Extra bonus: you can see exactly what part of the program needs the open file.
A file opened using a with
block will be closed as soon as execution leaves the block,
even if an exception is raised.
Always open files using with
, and make the body as short as possible.
Think of files like refrigerators: Open them for the shortest time possible.
Other OO languages often recommend RAII:
Resource Acquisition
is Instantiation.
Making an instance of a class acquires a resource, which is held for the lifetime of the object.
The resource is then freed by the class destructor when the object is deleted.
Python deletes objects you can no longer access (garbage collection) but:
Thus Python's with
blocks are a substitute for RAII.
Any object whose class is a context manager can be used in a
with
-block.
A context manager is a class with special methods:
__enter__
to perform setup__exit__
to perform cleanupContext managers are appropriate for:
__enter__(self):
as
in with
statement.__exit__(self,exc_type,exc,tb):
with
block.Expect each method to be called exactly once.
Some examples (listed as class - resource)
open
- Open filethreading.Lock
- Thread-exclusive righturllib.request.urlopen
- HTTP connectiontempfile.TemporaryFile
- Temporary file (deleted after use)