Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'help?': 0.03; 'context': 0.04; 'socket': 0.04; 'client:': 0.09; 'closed.': 0.09; 'contextlib': 0.16; 'eckhardt': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'seems': 0.20; 'cc:no real name:2**0': 0.20; 'dec': 0.22; "doesn't": 0.22; 'assume': 0.22; 'header:In-Reply- To:1': 0.22; 'filing': 0.23; 'cc:2**0': 0.24; 'code': 0.25; 'module': 0.26; 'coding': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'class': 0.29; 'seem': 0.30; 'received:209.85.210.46': 0.30; 'received:mail- pz0-f46.google.com': 0.30; 'tue,': 0.32; 'skip:@ 10': 0.34; 'http': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'could': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'difficult': 0.39; 'received:209': 0.40; 'might': 0.40; '2011': 0.61; 'your': 0.61; 'manager,': 0.62; 'skip:+ 10': 0.66; 'reorganize': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=joI7fcVmfrnss2XOFEPPgXes3c9bHEQ5Z1UsqZ4ZJDA=; b=TGIGGYTfenq2Ppw9PeL3iAYAOd1FoAhDyTHARmFA9Bg+pjZydN4PYid6aC6R2sqEht ep9YIzR/P1/ATBcR5uWu/q6y3JLB5i5RYnfdnANxJ/DMU2+itOMMWyqXqsH4iV37p3TT GjyAfFianmC1JJTsSielFzo+nz3Irhnu0rkcA= MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 20 Dec 2011 16:25:58 +0000 Subject: Re: nesting context managers From: Rami Chowdhury To: Ulrich Eckhardt Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324398361 news.xs4all.nl 6850 [2001:888:2000:d::a6]:46704 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17588 On Tue, Dec 20, 2011 at 14:15, Ulrich Eckhardt wrote: > Hi! > > Let us assume I had a class HTTPClient that has a socket for HTTP and a > logfile for filing logs. I want to make this HTTPClient a context manager= , > so that I can write > > =C2=A0with HTTPClient(url) as client: > =C2=A0 =C2=A0 =C2=A0pass > > and reliably have both the socket and the logfile closed. The easy way is > wrong > > =C2=A0def __enter__(self): > =C2=A0 =C2=A0 =C2=A0with self._mysock, self._myfile: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return self > It seems like some of the functions in the contextlib module might help? You could try and reorganize your code so that you can use the @contextmanager decorator, for instance? That having been said, it doesn't seem that difficult to me to code your own simple __exit__ method if you're already coding up __enter__ ? HTH, Rami --=20 Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544