【发布时间】:2014-11-18 11:51:21
【问题描述】:
不确定这有多大可能,但这里是:
我正在尝试编写一个具有更微妙行为的对象 - 这可能是一个好主意,也可能不是一个好主意,我还没有确定。
我有这个方法:
def __getattr__(self, attr):
try:
return self.props[attr].value
except KeyError:
pass #to hide the keyerror exception
msg = "'{}' object has no attribute '{}'"
raise AttributeError(msg.format(self.__dict__['type'], attr))
现在,当我像这样创建一个这样的实例时:
t = Thing()
t.foo
我得到一个包含 my 函数的堆栈跟踪:
Traceback (most recent call last):
File "attrfun.py", line 23, in <module>
t.foo
File "attrfun.py", line 15, in __getattr__
raise AttributeError(msg.format(self._type, attr))
AttributeError: 'Thing' object has no attribute 'foo'
我不希望这样 - 我希望读取堆栈跟踪:
Traceback (most recent call last):
File "attrfun.py", line 23, in <module>
t.foo
AttributeError: 'Thing' object has no attribute 'foo'
这是否可以通过最少的努力实现,还是需要很多?我找到了this answer,这表明某些事情看起来是可能的,尽管可能涉及。如果有更简单的方法,我很想听听!否则我就暂时搁置这个想法。
【问题讨论】:
-
我强烈建议不要这样做。一两个月后,您将忘记您的函数在何处以及为何抛出此特定异常。
-
如果你只想要一个自定义打印使用模块回溯,如果你想要一个干净的回溯,就让它这样。为什么要使用其他 dict 作为属性容器? self.__dict__ 已经存在。并且更改回溯并不简单,纯python不可能,你需要用ctypes破解解释器
-
@ivan_pozdeev 一方面这似乎是一件坏事,另一方面 - 我不知道为什么我会在阅读
AttributeError: 'X' has no attribute 'Y'时感到困惑。 -
@WayneWerner 您仍然需要知道这恰好说明了什么:哪个对象是“X”以及为什么要查询它以获取“Y”(如果涉及反射,则会更加混乱)。
-
不要忘记代码也可以自己抛出异常 - 有时,那些你意想不到的地方。