Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'context': 0.04; 'instance,': 0.05; 'interpreter': 0.05; 'parameter': 0.05; 'attribute': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '*function*': 0.09; 'dynamically': 0.09; 'foo': 0.09; 'foo,': 0.09; 'parameter.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'underlying': 0.09; 'am,': 0.12; 'def': 0.13; 'definition,': 0.16; 'manager)': 0.16; 'outputs': 0.16; 'reedy': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'jan': 0.19; '(most': 0.21; 'mechanism': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'wrong?': 0.23; 'steve': 0.24; 'fine': 0.24; 'traceback': 0.24; 'says': 0.25; "i'm": 0.26; 'expect': 0.26; 'function': 0.27; 'variable': 0.28; 'subject:" ': 0.28; 'pass': 0.29; 'error': 0.29; 'pm,': 0.29; 'class': 0.29; 'object.': 0.30; 'python3': 0.30; '(the': 0.30; 'does': 0.32; 'tue,': 0.32; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'object': 0.33; 'to:addr :python-list': 0.34; 'eric': 0.34; '17,': 0.34; 'last):': 0.34; 'requirement.': 0.34; 'url:python': 0.36; 'file': 0.36; 'subject:with': 0.36; 'bound': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'doing': 0.38; 'using': 0.38; 'received:org': 0.38; 'url:docs': 0.39; 'url:org': 0.39; 'called': 0.40; 'subject: (': 0.40; 'to:addr:python.org': 0.40; '2011': 0.61; 'full': 0.62; 'here': 0.65; 'special': 0.68; 'below:': 0.80; 'howell': 0.84; 'tricky': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'snow': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: AttributeError in "with" statement (3.2.2) Date: Wed, 14 Dec 2011 01:29:13 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323844169 news.xs4all.nl 6974 [2001:888:2000:d::a6]:42587 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17182 On 12/14/2011 1:05 AM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: >> I'm using Python 3.2.2, and the following program gives me an error >> that I don't understand: >> >> class Foo: >> pass >> >> foo = Foo() >> foo.name = "Steve" >> >> def add_goodbye_function(obj): >> def goodbye(): >> print("goodbye " + obj.name) >> obj.goodbye = goodbye >> >> add_goodbye_function(foo) >> foo.goodbye() # outputs goodbye Steve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve foo.goodbye, aliased as foo.__exit__, is a *function* attribute of the foo *instance*. >> with foo: # fails with AttributeError: __exit__ >> print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine when I call it directly, but it fails when I try to >> use foo as the expression in the with statement. Here is the full >> output: >> >>> python3 with.coffee >> goodbye Steve >> goodbye Steve >> Traceback (most recent call last): >> File "with.coffee", line 17, in >> with foo: # fails with AttributeError: >> AttributeError: __exit__ >> >> What am I doing wrong? To complement what Eric says below: The with statement is looking for an instance *method*, which by definition, is a function attribute of a *class* (the class of the context manager) that takes an instance of the class as its first parameter. Notice that goodbye does not take any parameter. The first parameter is conventionally called 'self', but that is not a requirement. That it be there, that it expect to be bound to an instance, and that the function be bound to the class are requirements. > That is a tricky one. > > As with many of the special methods (start and end with __) in Python, > the underlying mechanism in the interpreter is directly pulling the > function from the class object. It does not look to the instance > object for the function at any time. See > http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. -- Terry Jan Reedy