Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
651e8e55
Commit
651e8e55
authored
Apr 23, 2006
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a (very) brief mention of the with statement to the end of chapter 8
parent
ddd4d236
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
3 deletions
+33
-3
Doc/tut/tut.tex
Doc/tut/tut.tex
+33
-3
No files found.
Doc/tut/tut.tex
View file @
651e8e55
...
...
@@ -941,9 +941,9 @@ with \function{str()}, conversion takes place using this default encoding.
u'abc'
>>> str
(
u"abc"
)
'abc'
>>> u"
äöü
"
>>> u"
�"
u'
\xe
4
\xf
6
\xfc
'
>>> str
(
u"
äöü
"
)
>>> str
(
u"
�"
)
Traceback
(
most recent call last
)
:
File "<stdin>", line
1
, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0
-
2
: ordinal not in range
(
128
)
...
...
@@ -955,7 +955,7 @@ that takes one argument, the name of the encoding. Lowercase names
for encodings are preferred.
\begin
{
verbatim
}
>>> u"
äöü
".encode
(
'utf
-
8
'
)
>>> u"
�".
encode
(
'utf
-
8
'
)
'
\xc
3
\xa
4
\xc
3
\xb
6
\xc
3
\xbc
'
\end
{
verbatim
}
...
...
@@ -3744,6 +3744,36 @@ In real world applications, the \keyword{finally} clause is useful
for releasing external resources
(
such as files or network connections
)
,
regardless of whether the use of the resource was successful.
\section
{
Predefined Clean
-
up Actions
\label
{
cleanup
-
with
}}
Some objects define standard clean
-
up actions to be undertaken when
the object is no longer needed, regardless of whether or not the
operation using the object succeeded or failed.
Look at the following example, which tries to open a file and print
its contents to the screen.
\begin
{
verbatim
}
for line in open
(
"myfile.txt"
)
:
print line
\end
{
verbatim
}
The problem with this code is that it leaves the file open for an
indeterminate amount of time after the code has finished executing.
This is not an issue in simple scripts, but can be a problem for
larger applications. The
\keyword
{
with
}
statement allows
objects like files to be used in a way that ensures they are
always cleaned up promptly and correctly.
\begin
{
verbatim
}
with open
(
"myfile.txt"
)
as f:
for line in f:
print line
\end
{
verbatim
}
After the statement is executed, the file
\var
{
f
}
is always closed,
even if a problem was encountered while processing the lines. Other
objects which provide predefined clean
-
up actions will indicate
this in their documentation.
\chapter
{
Classes
\label
{
classes
}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment