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


Groups > comp.lang.python > #44648

Debugging difficulty in python with __getattr__, decorated properties and AttributeError.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <titanix88@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '"""': 0.07; 'attribute': 0.07; "'a'": 0.09; 'comments?': 0.09; 'def': 0.12; '"b":': 0.16; '@property': 0.16; 'alphabet': 0.16; 'name):': 0.16; 'subject: \n ': 0.16; 'unhelpful': 0.16; 'subject:python': 0.16; 'demonstrate': 0.16; 'exception': 0.16; 'import': 0.22; 'this:': 0.26; 'pass': 0.26; 'gets': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'produces': 0.31; 'file': 0.32; 'class': 0.32; 'running': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'skip:& 10': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'easy': 0.60; 'identify': 0.61; 'name': 0.63; 'due': 0.66; '20,': 0.68; '26,': 0.68; 'subject:skip:A 10': 0.78
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=fHZODsOo313n9UWo7bwW4vEM1nNLRPzTOa8X2mlc358=; b=kYp5e0IOy2zVb+cGZL6S38lOHHOop0TEIqtCTPNvJS8+22KoUm43t5HbP1OM47sh9V 3XM2VLwZj+yIPJbHE9iW+PBKP7z7N24t0UBc3ws+pD9Bw6DFuHSNwhe/Th++JGEeoMz4 3MZ7XI88WqrIdxeTYnXTxXgDHFLmhV4VqL0V2wmSDhGu7ODIle9D7OAhRiTOTxQlsLVe tIYR7avwjW2RzrCypyZpst46Tio/wBu/D3B1GXGRWHZAvupubEq7Pu7LX7CUA/WPsrDM TgDsLLsdfd+pAzKCURrfgBx6dkec9EqMKWcu+9quJc/KCx/5EQ/Aw2WEwdrZBKLIqdvt sztQ==
MIME-Version 1.0
X-Received by 10.66.144.136 with SMTP id sm8mr11969872pab.115.1367537680961; Thu, 02 May 2013 16:34:40 -0700 (PDT)
Date Fri, 3 May 2013 05:34:40 +0600
Subject Debugging difficulty in python with __getattr__, decorated properties and AttributeError.
From "Mr. Joe" <titanix88@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7b6d826eb0d6dc04dbc4ad99
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.1257.1367537690.3114.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1367537690 news.xs4all.nl 15916 [2001:888:2000:d::a6]:44965
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44648

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Is there any way to raise the original exception that made the call to
__getattr__? I seem to stumble upon a problem where multi-layered attribute
failure gets obscured due to use of __getattr__. Here's a dummy code to
demonstrate my problems:
"""
import traceback


class BackupAlphabet(object):
    pass


class Alphabet(object):
    @property
    def a(self):
        return backupalphabet.a


    def __getattr__(self, name):
        if name == "b":
            return "banana"

        raise AttributeError(
            "'{0} object has no attribute '{1}'"
            .format(self.__class__.__name__, name))


alphabet = Alphabet()
backupalphabet = BackupAlphabet()

print(alphabet.a)
print(alphabet.b)
"""

Running the above code produces this:
"""
Traceback (most recent call last):
  File "example.py", line 26, in <module>
    print(alphabet.a)
  File "example.py", line 20, in __getattr__
    .format(self.__class__.__name__, name))
AttributeError: 'Alphabet object has no attribute 'a'
"""

While it's easy enough to identify the problem here, the traceback is
rather unhelpful in complex situations. Any comments?

Regards,
TB

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


Thread

Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-03 05:34 +0600
  Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-03 00:20 +0000
    Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-03 13:52 +0600
      Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-04 06:29 +0000
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-14 21:09 +0600
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. dieter <dieter@handshake.de> - 2013-05-15 08:15 +0200
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-15 22:38 +0600
    Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. dieter <dieter@handshake.de> - 2013-05-04 07:56 +0200

csiph-web