classification
Title: enhacement proposal in howto/doanddont
Type: Stage: needs patch
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Adrián.Deccico, docs@python, eric.araujo, georg.brandl
Priority: normal Keywords: patch

Created on 2010-04-24 17:57 by Adrián.Deccico, last changed 2010-04-25 10:17 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
doanddont.rst.patch Adrián.Deccico, 2010-04-24 19:14 patch for "Doc/howto/doanddont.rst"
Messages (4)
msg104111 - (view) Author: Adrián Deccico (Adrián.Deccico) Date: 2010-04-24 17:57
Hi, in "Exceptions" section. 

Instead of using:

def get_status(file):
    fp = open(file)
    try:
        return fp.readline()
    finally:
        fp.close()

Why no suggest this method:

def get_status(file):
    with open(file) as fp:
	return fp.readline()


which will properly close the file.
msg104112 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-04-24 18:04
Hello

You’re right, this idiom (new in 2.5, always enabled in 2.6) is now the recommended way of doing this. Note that the older code does close the file properly too, it’s just another way of doing it.

Would you like to provide a patch against the latest version of this file?
 
Regards
msg104119 - (view) Author: Adrián Deccico (Adrián.Deccico) Date: 2010-04-24 19:14
Hi, you are right. I am attaching the patch. I tested against 2.6.5 and trunk (the file has no changed)

thanks
msg104146 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-04-25 10:17
Thanks, applied in r80461.
History
Date User Action Args
2010-04-25 10:17:48georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg104146

resolution: fixed
2010-04-24 19:14:10Adrián.Deccicosetfiles: + doanddont.rst.patch
keywords: + patch
messages: + msg104119
2010-04-24 18:04:49eric.araujosetassignee: docs@python ->

title: enhacement proposal in http://docs.python.org/howto/doanddont.html -> enhacement proposal in howto/doanddont
nosy: + eric.araujo
versions: + Python 3.1, Python 2.7, Python 3.2
messages: + msg104112
stage: needs patch
2010-04-24 17:57:34Adrián.Deccicocreate