[Python-Dev] List vs Tuple / Homogeneous vs Heterogeneous / Mutable vs Immutable

Chris Barker chris.barker at noaa.gov
Thu Apr 24 18:53:52 CEST 2014


Ooops,

Forgot "reply all" last time -- here it is again.

On Wed, Apr 23, 2014 at 3:17 PM, Chris Barker <chris.barker at noaa.gov> wrote:

> On Mon, Apr 21, 2014 at 11:39 PM, Raymond Hettinger <
> raymond.hettinger at gmail.com> wrote:
>
>> In fact, the distinction is extrinsic to their implementations.  It is
>> only important
>> because the rest of the language tends to treat them differently.  For
>> example,
>> you could store ['raymond', 'red'] as a list or as a tuple ('raymond',
>> 'red'), but you
>> wouldn't be punished until later when you tried:
>>
>>      'I think %s likes %s' % container     # str.__mod__ treats lists and
>> tuples differently
>>
>
> I've been bitten by that a lot -- I always wondered why that didn't work
> with any sequence. like "tuple unpacking", which is really sequence
> unpacking:
>
> x, y = [3,4]
>
> But anyway, when I teach Python, I do struggle with this issue -- I tend
> to give the explanation of "structs" vs. "homogenous sequences", but I feel
> like I am repeating a party line, rather than anything useful.  What I do
> is:
>
> If it needs to be immutable (dict key), then use a tuple
> If it needs to be mutable, then use a list
>
> Otherwise, you can use either, but I tend to use a tuple for small things
> I don't need to mutate, regardless of whether they are homogenous or not --
> it makes me feel better to get the perception of a *tiny* bit more
> efficiency.
>
> And sometimes you want to mutate small collections of inhomogeneous values
> (C sructs are mutable, after all).
>
> So I don't think this needs to be codified this in the docs anywhere.
>
>
>>  Likewise, there seems to be wide-spread confusion about make makes an
>> object immutable.  People seem to miss that ints, tuples, None and str are
>> immutable only because they lack any mutating methods,
>>
>
> not sure your point here -- that under the hood one could mutate them with
> C code?
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.go <Chris.Barker at noaa.gov>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140424/f46441cf/attachment.html>


More information about the Python-Dev mailing list