function object.func_default off the console

Peter Otten __peter__ at web.de
Wed Apr 25 08:56:24 CEST 2007


Aaron Brady wrote:

>>>> f.func_defaults[0]
> [2, 3]
>>>> f.func_defaults[0]+=[4]
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: 'tuple' object does not support item assignment
>>>> f.func_defaults[0]
> [2, 3, 4]
> 
> V. interesting.  Operation succeeds but with a throw.  Er, raise.

This is not specific to func_defaults:

>>> t = ([1],)
>>> t[0] += [2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t
([1, 2],)

t[0] += [2]

is resolved to

t.__setitem__(t[0].__iadd__([2]))

where list.__iadd__() succeeds but __setitem__() fails (because tuples don't
have that method).

Peter



More information about the Python-list mailing list