Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17181

Re: AttributeError in "with" statement (3.2.2)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ericsnowcurrently@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'interpreter': 0.05; 'attribute': 0.07; 'python': 0.08; 'dynamically': 0.09; 'foo': 0.09; 'foo,': 0.09; 'statement.': 0.09; 'underlying': 0.09; 'def': 0.13; 'received:209.85.210.174': 0.13; 'received:mail-iy0-f174.google.com': 0.13; 'outputs': 0.16; '\xa0def': 0.16; '\xa0here': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.20; '(most': 0.21; 'mechanism': 0.21; 'dec': 0.22; 'header:In-Reply- To:1': 0.22; 'wrong?': 0.23; 'steve': 0.24; 'cc:2**0': 0.24; 'fine': 0.24; 'traceback': 0.24; "i'm": 0.26; 'function': 0.27; 'variable': 0.28; 'subject:" ': 0.28; 'message- id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'error': 0.29; 'pm,': 0.29; 'class': 0.29; 'object.': 0.30; 'python3': 0.30; 'does': 0.32; 'tue,': 0.32; 'object': 0.33; '17,': 0.34; 'last):': 0.34; 'url:python': 0.36; 'subject:with': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'doing': 0.38; 'using': 0.38; 'received:209.85': 0.38; 'skip:\xa0 10': 0.39; 'url:docs': 0.39; 'url:org': 0.39; 'subject: (': 0.40; 'received:209': 0.40; '8bit%:8': 0.40; '2011': 0.61; 'full': 0.62; 'special': 0.68; 'to:addr:yahoo.com': 0.83; 'howell': 0.84; 'tricky': 0.84; 'url:datamodel': 0.84; 'url:reference': 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=0VOOl5HrvXQedFmp9u7xEov0yKifQX7YUOijPt0n1DE=; b=qIS2tlRA6+MVjO0NwMm4/zOMoNedRnCpzUWghavytxxigmwcr/Lix2eprv1tI9eila 0e8jQP8xNV2TwPF95LjC+hqK5T9bpeUP2GBZjZRCtSSxlNWIsf35MDMnQFKbGQidzYEf VCbKGtKR9Osp5OGFC604qzPXRf3L6JdsUYQPs=
MIME-Version 1.0
In-Reply-To <b8092181-c306-40bc-a07a-bb35bc925cf1@18g2000prn.googlegroups.com>
References <b8092181-c306-40bc-a07a-bb35bc925cf1@18g2000prn.googlegroups.com>
Date Tue, 13 Dec 2011 23:05:53 -0700
Subject Re: AttributeError in "with" statement (3.2.2)
From Eric Snow <ericsnowcurrently@gmail.com>
To Steve Howell <showell30@yahoo.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3625.1323842756.27778.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323842756 news.xs4all.nl 6986 [2001:888:2000:d::a6]:50949
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17181

Show key headers only | View raw


On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell <showell30@yahoo.com> 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
>
> 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 <module>
>    with foo: # fails with AttributeError:
> AttributeError: __exit__
>
> What am I doing wrong?

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.

-eric

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-13 21:42 -0800
  Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-13 23:05 -0700
  Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-14 01:29 -0500
    Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-14 08:01 +0000
      Re: AttributeError in "with" statement (3.2.2) 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-14 08:08 -0800
        Re: AttributeError in "with" statement (3.2.2) 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-14 08:28 -0800
      Re: AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-14 09:16 -0800
      Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-14 18:13 -0500
        Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 05:01 +0000
          Re: AttributeError in "with" statement (3.2.2) MRAB <python@mrabarnett.plus.com> - 2011-12-15 05:15 +0000
            Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 07:21 +0000
            Re: AttributeError in "with" statement (3.2.2) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-12-16 09:34 +1300
          Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 07:47 +0000
          Re: AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-15 05:35 -0800
            Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 03:34 +0000
          Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-15 19:39 -0500
            Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 09:22 +0000
              Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-16 17:05 -0500
                Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-17 01:26 +0000
                Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-17 21:09 -0500
              Re: AttributeError in "with" statement (3.2.2) Ethan Furman <ethan@stoneleaf.us> - 2011-12-16 15:26 -0800
                Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-17 03:05 +0000
              Re: AttributeError in "with" statement (3.2.2) Ethan Furman <ethan@stoneleaf.us> - 2011-12-16 16:34 -0800
  Re: AttributeError in "with" statement (3.2.2) Peter Otten <__peter__@web.de> - 2011-12-14 11:02 +0100
  Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-14 09:56 -0700
  Re: AttributeError in "with" statement (3.2.2) Lie Ryan <lie.1296@gmail.com> - 2011-12-15 06:14 +1100
  Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-14 12:46 -0700

csiph-web