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

ZhangXiang zhangyangyu0614 at gmail.com
Mon Mar 7 03:46:40 EST 2016


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.



More information about the Python-list mailing list