var or inout parm?

Arnaud Delobelle arnodel at googlemail.com
Fri Dec 12 14:26:27 EST 2008


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> writes:

> On Fri, 12 Dec 2008 07:56:58 -0800, sturlamolden wrote:
>
>> On Dec 12, 4:55 pm, sturlamolden <sturlamol... at yahoo.no> wrote:
>> 
>>> def __setitem__(self, index, value):
>>>    if _buf[index] is not value: # given that _buf is the tuple's
>>> internal buffer
>>>       raise TypeError, 'tuple' object does not support item
>>> assignment
>> 
>> blæh, that should be self._buf[index]
>
> But then the error message is not true anymore because tuples *would* 
> support item assignment when the old and new value are the same.  Which 
> leads to strange things like code that may or may not raise that 
> exception, depending on implementation details:
>
> t = (1, 2)
> t[0] = 1   # Maybe okay -- maybe not.
> t[1] += 0  # Same here.
>
> I'd find that quite odd.
>
> Ciao,
> 	Marc 'BlackJack' Rintsch

What I find a bit annoying is when you get both

* an exception
* a mutation

E.g.

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

Now is t the same as before?  Sometimes it is:

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

Sometimes not:

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

I agree it's not a bug, but I never feel comfortable with it.

-- 
Arnaud



More information about the Python-list mailing list