Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.straub-nv.de!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.044 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'python': 0.08; 'received:209.85.160.174': 0.09; 'received:mail- gy0-f174.google.com': 0.09; 'am,': 0.12; 'attribute:': 0.16; 'magic.': 0.16; 'metaclasses': 0.16; 'superfluous': 0.16; 'cc:addr :python-list': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'ryan': 0.18; 'cc:no real name:2**0': 0.20; "aren't": 0.21; 'dec': 0.22; 'header :In-Reply-To:1': 0.22; 'opens': 0.23; 'steve': 0.24; 'cc:2**0': 0.24; 'originally': 0.24; 'subject:" ': 0.28; 'cloud': 0.28; 'needed,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'explicit': 0.29; "skip:' 10": 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'definition': 0.30; 'rarely': 0.30; 'tue,': 0.32; 'sort': 0.33; 'received:209.85.160': 0.33; 'eric': 0.34; 'approach.': 0.34; 'lie': 0.34; 'however,': 0.36; 'question': 0.36; 'subject:with': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'relatively': 0.39; 'being': 0.39; 'tool': 0.39; 'subject: (': 0.40; 'received:209': 0.40; 'might': 0.40; 'more': 0.61; '2011': 0.61; 'worth': 0.61; 'your': 0.61; 'world': 0.62; 'details': 0.65; 'works,': 0.68; '(make': 0.84; '03:56': 0.84; '12:14': 0.84; 'hand.': 0.84; 'snow': 0.91 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=p8xmorOgS3j/U7rSEDh9xwDvGUIEE+SElMuOREOhDp4=; b=CRIHeaGkrBMBjUAiMK7ycCXtjEUx/3E95khBgMj98N7CMPugiHJ34614my+B7bUCtn NAbGusiwIJJ5QpkxiJ+iRsnzngGxUYLCGGp80NwsqWaGB/58rUiZC46ngRxBc1WxAU9t wMP+AlKS+DKBUsP2Lmok0Bv0cLke/F94EDTx0= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 14 Dec 2011 12:46:58 -0700 Subject: Re: AttributeError in "with" statement (3.2.2) From: Eric Snow To: Lie Ryan Content-Type: text/plain; charset=ISO-8859-1 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323892021 news.xs4all.nl 6868 [2001:888:2000:d::a6]:54749 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17231 On Wed, Dec 14, 2011 at 12:14 PM, Lie Ryan wrote: > On 12/15/2011 03:56 AM, Eric Snow wrote: >> >> On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow >> =A0wrote: >> >> If you want to be more dynamic about it you can do it, but it involves >> black magic. =A0Chances are really good that being explicit through your >> class definition is the right approach. > > > Note that the black spice is to use the __class__ attribute: > > foo.__class__.__exit__ =3D foo.goodbye Good point. 'type(foo).__exit__ ...' might be even better. Regardless, to get the dynamicism to which Steve originally referred (make it work when added directly to the instance), you have to use a metaclass, which is black magic[1]. However, rarely is that sort of thing needed, so the relatively superfluous details would likely only cloud the question at hand. -eric [1] I will point out that metaclasses aren't really all that bad. They are a tool worth understanding, even if you don't use them all the time. Understanding them also opens up a whole new world of understanding how Python works, particularly regarding name lookup.