args attribute of an exception subclass's type changes from string to tuple

Viewed 331

This happens on both python2.6 and python3:

class Error(Exception):
    def __init__(self, args):
            print(type(args))
            print(type(self.args)) # From BaseException
            self.args = args
            print(type(self.args))

Error("foo")

This results in:

<type 'str'>
<type 'tuple'>
<type 'tuple'>
Error('f', 'o', 'o')

For some reason, the args attribute is coerced into a tuple. Might the fact that it's defined in C have something to do with it? https://github.com/python/cpython/blob/master/Objects/exceptions.c

The name of the args argument is unrelated. Changing it to 'a' results in the same behavior, as long as it is assigned to self.args.

1 Answers
Related