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...)
Method 1: List Comprehension + list() The recommended way to convert a tuple of tuples to a list of lists is using list comprehension in combination with the built-in list() function like so: [list(x) for x in tuples] .
The primary difference between tuples and lists is that tuples are immutable as opposed to lists which are mutable. Therefore, it is possible to change a list but not a tuple. The contents of a tuple cannot change once they have been created in Python due to the immutability of tuples.
No, tuples and lists should not be used interchangeably. Two reasons: 1) There's a very practical difference, lists are mutable and tuples aren't, so there's a whole bunch of methods, like append which do not exist on tuples . 2) tuples and lists represent something semantically different.
Inbuilt tuple() function, also known as a constructor function, can be used to convert a list to a tuple. We can use for loop inside the tuple function to convert the list into a tuple. It is faster to convert the list to a tuple in python by unpacking the list inside parenthesis.
Exception.args
is a descriptor; it hooks __set__
to turn anything you assign to self.args
into 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 your list()
call failed, it's just that Exception.args
is special.
BaseException.args
is documented to be a tuple, and in Python 2, exception objects support slicing:
>>> ex = Exception(1, 2)
>>> ex.args
(1, 2)
>>> ex[0]
1
Exceptions are also supposed to be immutable; keeping the .args
attribute a tuple helps keep them so. Moreover, the __str__
handler for exceptions expects .args
to be a tuple, and setting it to something else has led to strange bugs in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With