coding style - zen of Python vs with statement - philosophical pondering -
i don't intend waste time, but: has occurred too, while using python's with statement contrary 5th line of "the zen of python" goes "flat better nested"? can enlightened python guru share me of insights on this?
(i find 1 more level of indentation pops in code every time use with instead of f.close()... , it's not i'm not gonna use try: ... finally: ... anyways , benefits of with still elude me, grow , understand python more , more...)
@glglgl (sorry, can't find way write code in comments): yes, if go with way, code becomes:
try: file(...) f: ... except ioerror: ... ...and using without try people end doing in type of hacky "one use" code use f.close() instead of anyways (which bad because file may not closed if exception thrown before f.close()), "hacky" code people don't use with because, don't know, guess find "fancy" , structured code doesn't bring benefits anyways, seems me there's no real world use case left it... pondering really.
yes, the zen of python states "flat better nested", not characteristic care about; states "simple better complex". beauty of with adheres both of principles explain below.
any time find in philosophical pondering feature in python it's worth looking python enhancement proposals (peps) read motivation behind feature. in case pep 343 -- "with" statement says front in abstract:
this pep adds new statement "with" python language make possible factor out standard uses of try/finally statements.
factoring out try/finally statements makes code simpler , more readable.
pep 343 goes deeper providing simplistic syntactic sugar, however. establishes context manager protocol:
the expression following keyword in statement "context expression" expression provides main clue runtime environment context manager establishes duration of statement body.
using context manager protocol, api writers can hide complexity , ensure correct acquisition/release of resources in multi-threaded context.
but real beauty of with statement shown in example 12 of pep 343 explains that:
a "nested" context manager automatically nests supplied contexts left-to-right avoid excessive indentation.
using nested() context manager can take code looks this:
with x: b y: c z: # perform operation and turn this:
with nested(a, b, c) (x, y, z): # perform operation note nested() introduced in python 2.5, of version 2.7 deprecated in favor of multiple context manager syntactic form:
with x, b y, c z: # perform operation clearly not simpler , more readable, more flat nested. thus, using with following path of ç¡ç² :)
update: in response comments on simeon visser's answer here example of when might use multiple context managers open more 1 file @ once, when want zip contents of 2 (or more) files such if opening 1 of files fails make whole thing fail , close each file opened:
from itertools import izip open("/etc/passwd") a, open("/etc/group") b, open("/etc/shadow") c: lines in izip(a,b,c): print map(lambda x: x.split(':')[0], lines) run example twice; once root , once normal user. presuming save file ziptogether.py first try invoking root sudo python ziptogether.py , succeed, invoking normal user python ziptogether.py fail because don't have permissions read /etc/shadow. when fails context manager ensure files opened before failure closed when execution moves outside scope of with statement.
Comments
Post a Comment