[Tutor] .get dicionary question (mutability??)

Emile van Sebille emile at fenx.com
Fri Jun 29 17:32:48 CEST 2012


On 6/29/2012 6:45 AM Albert-Jan Roskam said...
> Hi Steven,
> Thanks for helping me. It makes more sense now. Calling append() on the
> datatype list returns None but calling __add__ on the datatype int
> returns, well, the result.

I don't know that it's consistent across all types, but I've found that 
mutable types tend to change in place and return None while immutable 
types tend to return the altered result, which is as you describe below.

Emile




> Somehow it makes more sense to me when
> writing + as __add__
> a[k] = a.get(k, 0).__add__(1)
> b[k] = b.get(k, []]).__add__([v])
>  >>> x = 1
>  >>> y = []
>  >>> z = y.append(x)
>  >>> z is None
> True
> Regards,
> Albert-Jan
>
>



More information about the Tutor mailing list