Lawful != Mutable (was Can Python function return multiple data?)

Chris Angelico rosuav at gmail.com
Sun Jun 7 12:07:54 EDT 2015


On Mon, Jun 8, 2015 at 1:49 AM, Rustom Mody <rustompmody at gmail.com> wrote:
> On Saturday, June 6, 2015 at 11:13:52 AM UTC+5:30, Chris Angelico wrote:
>> On Sat, Jun 6, 2015 at 3:28 PM, Rustom Mody  wrote:
>> > You just repeated what Chris said, replacing 'immutable' with 'same'
>> > There was a list: [1,2,3]
>> > At some point that list is found to be(come) [1,2,3,4]
>> > They dont look same to me.
>>
>> "I'm going shopping, can you get me the shopping list please?"
>> *goes and fetches a piece of paper from the kitchen*
>> "Wait, this isn't the right list! This one has more things on it!"
>>
>> The question of whether or not the thing fetched is indeed the
>> shopping list is independent of the items on it. The list has an
>> identity and it has a value (the items needed). If I hand you an empty
>> list on the basis that the shopping list you placed there last week
>> was empty, I've destroyed the value of the posted shopping list -
>> people have added things to it during the week, and they expect those
>> things to be on the list that gets referenced to make purchases.
>
>
> Well that is a more useful metaphor than much of what I see being said here.
> [Most of what I see being summarizable into:
> This is the way python is
> Python is Holy Writ
> THOU SHALT NOT QUESTION
> ]
>
> So thanks Chris for a non-pythonic metaphor.
>
> May we look a little into it?
>
> Is your shopping list really a list? If I have:
>
> 1. Bread
> 2. Eggs
> 3. Rice
>
> Presumably that's the 'same' list as
>
> 1. Eggs
> 2. Bread
> 3. Rice
>
> IOW if we have to make a python data-structure for it, the (python) list
> ["eggs", "bread", "rice"]
> is overspecific. Whereas the set
> set(["eggs", "bread", "rice"])
> works better for the simple reason that python will conform with our informal
> expectation of the two differently listed shopping lists being
> equal with the set data structure and not the list data structure.  ie
> ["eggs", "bread", "rice"] == ["bread", "eggs", "rice"] fails
> whereas
> set(["eggs", "bread", "rice"]) == set(["bread", "eggs", "rice"]) works

Well, yes and no. I used the term "list" because most people don't
talk about a "shopping set", and the list that's posted does have an
order to it; the effect of the list will be the same regardless of
order, but the order might be useful for curiosity purposes - the
items at the head of the list have been on it longer than the ones at
the tail, so they most likely will be more needed. For a comparison,
I'm sure you'd agree that the order of a program's arguments matters,
so you need a list and not a set; but if you type "cp spam eggs ham
target/" it's going to have the same net result as "cp eggs spam ham
target/", in that all three files will be copied to the same target
directory.

There are plenty of real-world lists where order turns out to not
matter, but they still retain that order. On any membership list, the
important thing is whether you're on it or not, but somewhere on a
sheet of paper, someone's going to be higher and someone's going to be
lower. A list of the different types of Cadbury Roses has to be sorted
one way or another; the Wikipedia page [1] mentions Cherry Heaven
above Turkish Delight, but my blog [2] has them the other way around -
is one of them somehow wrong? These are, in a way, sets masquerading
as lists, but given that many contexts don't really allow any
distinction, it's not usually considered to be a problem.

So you can call them sets if you like, but there's no real
significance either way.

ChrisA

[1] http://en.wikipedia.org/wiki/Cadbury_Roses
[2] http://rosuav.blogspot.com.au/2012/06/scientific-research-into-cadbury-roses.html



More information about the Python-list mailing list