Skip to content
This repository has been archived by the owner on Jan 3, 2018. It is now read-only.

Commit

Permalink
merging in Leszek's changes to gh-pages
Browse files Browse the repository at this point in the history
Thanks to @czterybity for this contribution, and to @wking for
mentoring this pull request.

Thanks to @r-gaia-cs, @mattphotonman, @gvwilson, @wltrimbl, and
 @ethanwhite for helpful commentary and feedback.
  • Loading branch information
ahmadia committed Dec 2, 2013
2 parents 630ac85 + 33ffd54 commit e74d529
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lessons/thw-python/strings-io/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"source": [
"# Strings\n",
"\n",
"**Prepared by Tommy Guy and Anthony Scopatz**\n",
"**Authors: Tommy Guy, Anthony Scopatz, Will Scopatz, Leszek Tarkowski**\n",
"\n",
"Lesson goals:\n",
"\n",
Expand Down Expand Up @@ -519,6 +519,33 @@
"each string in the list, add it yourself."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Context managers\n",
"\n",
"Closing a file is something often neglected in Python due to the fact that\n",
"it is usually done automatically at the end of a script by Python's garbage collector,\n",
"a part of the interpreter responsible for closing unused resources.\n",
"\n",
"In less trivial scenarios, a file should be closed after using it to prevent\n",
"data corruption. To ensure this, you can use a special language construct\n",
"called a context manager, available since Python 2.5.\n",
"\n",
"```python\n",
" with open('outfile.txt','w') as f:\n",
" f.write(\"Message of a Great Importance\")\n",
"\n",
" #other instructions, file is already closed at this point\n",
"```\n",
"\n",
"Also called \"with statements\", context managers are responsible for releasing\n",
"resources when they are no longer needed. In this example, the context manager\n",
"opens a file, creates a file handle variable to the open file named `f`, and after\n",
"the block of instructions completes, closes the file."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit e74d529

Please sign in to comment.