I need to know WHY this fails:
class ConfigurationError(Exception):
def __init__(self, *args):
super(ConfigurationError, self).__init__(self, args)
self.args = list(args)
# Do some formatting on the message string stored in self.args[0]
self.args[0]=self.__prettyfi(self.args[0])
def __prettyfi(self, arg):
pass
# Actual function splits message at word
# boundaries at pos rfind(arg[1:78]) if len(arg) >78
# it does this by converting a whitespace char to a \n
When I run the code, i receive the following msg:
<snip>
ConfigurationError.py", line 7, in __init__
self.args[0]=self.__prettyfi(self.args[0])
TypeError: 'tuple' object does not support item assignment
I edited the line no. to match this code sample.
I do not understand why self.args = list(args) does not correctly unpack the tuple into the list at line 5.
(I have a sneaking suspicion I am failing to remember something super-basic…)
Exception.argsis a descriptor; it hooks__set__to turn anything you assign toself.argsinto a tuple.So, as soon as you assign your list to
self.args, the descriptor converts it back to a tuple. It’s not that yourlist()call failed, it’s just thatException.argsis special.BaseException.argsis documented to be a tuple, and in Python 2, exception objects support slicing:Exceptions are also supposed to be immutable; keeping the
.argsattribute a tuple helps keep them so. Moreover, the__str__handler for exceptions expects.argsto be a tuple, and setting it to something else has led to strange bugs in the past.