Python: Maximum recursion depth exceeded when printing custom exception

a 夏天 提交于 2019-12-05 02:45:25

Your super invocation is wrong: self should not be supplied again, it's already injected by super. This way, file_error.args[0] is file_error because you pass self as an extra argument to the exception constructor. This should make it obvious why fix #1 (removing the super call altogether) helps, but of course the best fix is to pass the right arguments:

super(FileError, self).__init__(filename, *a, **k)

The reason for the infinite recursion: First off, only object.__str__ delegates to __repr__; BaseException defines both __str__ and __repr__ separately, so str() of an exception calls that overload, not your __repr__. BaseException.__str__ usually prints the args tuple (which would use repr), though when it contains a single argument, it prints the str() of that single argument.

This invokes BaseException.__str__ again, and so on. Fix #2 prevents this cycle by not entering BaseException.__str__ in the first place, instead using your __repr__ which does not touch the args tuple at all.

This line is incorrect:

super( FileError, self ).__init__( self, *a, **k )

You need to pass self in super() but not again as argument to __init__. So it needs to be:

super( FileError, self ).__init__( *a, **k )

Don't pass self to __init__ as the first argument. That's causing the recursion.

It should be:

super( FileError, self ).__init__( filename, *a, **k )

The recursion is caused because

>>> print Exception("Abc")
Abc

Exception prints the first argument. So when you initialize the base class of FileError i.e. Exception with self which inherits __str__ from it's parent which prints the first argument (hope you see the recursion in the statement).. hence you get the infinite recursion.

__str__ = __repr__ overrides the inherited __str__ and mitigates the infinite recursion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!