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.
Looking at the code you link to, there is a setter defined for the "args" attribute. Look for BaseException_set_args - it is being set (elsewhere in the linked code) as the setter for args. So when you write self.args = args
, you're really calling the function BaseException_set_args, with args
as the argument.
If you then look in BaseException_set_args, it is coercing the argument into a tuple. If you give try to set self.args to something that can't be converted to a tuple (e.g. try Error(23)
), you'll get a TypeError.
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