Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: jmp Newsgroups: comp.lang.python Subject: Re: How to get which attribute causes the AttributeError except inspecting strings? Date: Mon, 07 Mar 2016 11:50:05 +0100 Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de tCXXKXH8szZEFwE+fV/Qsgiu5aVaizTu6EkXBWOprNhg== Return-Path: 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; 'raises': 0.07; 'subject:How': 0.09; "%s'": 0.09; 'field)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'exception': 0.13; 'subject: \n ': 0.15; 'attr': 0.16; 'handling.': 0.16; 'hint?': 0.16; 'httperror,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'something.': 0.16; 'valid.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; 'try:': 0.18; 'variable': 0.18; 'parse': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'raise': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'anyone': 0.32; 'maybe': 0.33; 'foo': 0.33; 'except': 0.34; 'could': 0.35; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'received:org': 0.37; 'someone': 0.38; 'hi,': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'field': 0.60; 'received:194': 0.61; 'telling': 0.61; 'charset:windows-1252': 0.62; 'strange': 0.63; 'subject:skip:A 10': 0.63; 'different': 0.63; 'miss': 0.77; 'subject:get': 0.81 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104205 On 03/07/2016 09:46 AM, ZhangXiang wrote: > In python3, when I write code like this: > > try: > fields = [getattr(Product, field) for field in fields.split(',')] > except AttributeError as e: > raise HTTPError(...) > > I want to raise a new type of error giving a string telling the user which attribute is not valid. But I don't see any method I can use to get the attribute name except inspecting e.args[0]. > > Could anyone give me a hint? Maybe I miss something. > > By the way, I don't quite want to change my code to a for-loop so I can access the field variable in exception handling. > Hi, It is strange to morph an AttributeError in an HTTPError, anyway, you could write try: fields = [getattr(Product, field) for field in fields.split(',')] except AttributeError as e: raise HTTPError(str(e)) Or if you really need to write some custom message, you could parse the attribute error message for the attribute name. # I've not tested this code !! import re try: fields = [getattr(Product, field) for field in fields.split(',')] except AttributeError as e: # e.message should look like "class Foo has no attribute 'a'" attr = re.search(r"'(\w+)'", e.message).group(1) raise HTTPError('Custom message for the attribute %s' % attr) Tbh I don't like it but it could do the job until someone raises you an attribute error with a different string pattern. jm