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


Groups > comp.lang.python > #104202

Re: How to get which attribute causes the AttributeError except inspecting strings?

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How to get which attribute causes the AttributeError except inspecting strings?
Date 2016-03-07 10:48 +0100
Organization None
Message-ID <mailman.12.1457344177.10335.python-list@python.org> (permalink)
References <be90a047-38ea-4108-adb8-17fd7483fee5@googlegroups.com>

Show all headers | View raw


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.

It seems that the name of the attribute is not made available. You have to 
parse the error message or provide the name yourself:

>>> def mygetattr(object, name):
...     try:
...         return getattr(object, name)
...     except AttributeError as err:
...         err.name = name
...         raise
... 
>>> class Product:
...     foo = 42
... 
>>> fields = "foo,bar"
>>> try:
...     fields = [mygetattr(Product, field) for field in fields.split(",")]
... except AttributeError as err:
...     print(err)
...     print("Missing attribute:", err.name)
... 
type object 'Product' has no attribute 'bar'
Missing attribute: bar

Raising a subclass instead of mutating and reraising the original 
AttributeError is probably cleaner...

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


Thread

How to get which attribute causes the AttributeError except inspecting strings? ZhangXiang <zhangyangyu0614@gmail.com> - 2016-03-07 00:46 -0800
  Re: How to get which attribute causes the AttributeError except inspecting strings? Peter Otten <__peter__@web.de> - 2016-03-07 10:48 +0100
    Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 02:15 -0800
  Re: How to get which attribute causes the AttributeError except inspecting strings? jmp <jeanmichel@sequans.com> - 2016-03-07 11:50 +0100
    Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 08:25 -0800
      Re: How to get which attribute causes the AttributeError except inspecting strings? Tim Golden <mail@timgolden.me.uk> - 2016-03-07 16:38 +0000
        Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 09:01 -0800
      Re: How to get which attribute causes the AttributeError except inspecting strings? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-07 17:26 +0000

csiph-web