Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python tuples and lists. A tuple that refuses to convert

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...)

like image 776
Jase Avatar asked Jan 07 '13 12:01

Jase


People also ask

How do you convert a tuple to a list of tuples in Python?

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] .

What is the difference between a Python tuple and Python list?

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.

Are tuples and lists interchangeable?

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.

Which method can be used to convert a list to a tuple Python?

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.


1 Answers

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.

like image 190
Martijn Pieters Avatar answered Sep 26 '22 21:09

Martijn Pieters