Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92261

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

Newsgroups comp.lang.python
Date 2015-06-07 08:49 -0700
References (14 earlier) <mailman.207.1433541572.13271.python-list@python.org> <81cb1232-ecf8-4cf0-b29c-ecf2ca47ade0@googlegroups.com> <mailman.209.1433561282.13271.python-list@python.org> <c58d7868-feaf-492e-9fb9-9b0108c5914d@googlegroups.com> <mailman.212.1433569420.13271.python-list@python.org>
Message-ID <5fa1f672-50de-4481-9c5b-2c68c4e30f12@googlegroups.com> (permalink)
Subject Lawful != Mutable (was Can Python function return multiple data?)
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


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

Is it only coincidentally related to the math law: 
{1,2,3} = {2,1,3}  ?

Now say we change the example slightly, from shopping list to
shopping itenary:
1. Grocer
2. Hardware
3. Clothes

and due to geography of the stores the above order is the best one.
[Yeah I am assuming the Travelling Buysman problem is a solved problem]


So I model that as
itinerary = ["grocer", "hardware", "clothes"]

O! I forgot... Need to cut my hair

itinerary[:2] + ["barber"] + itinerary[-1:]
['grocer', 'hardware', 'barber', 'clothes']
>>> 

Nice... Just one doubt
Do I need to parenthesize that expression??

I try
>>> itinerary[:2] + (["barber"] + itinerary[-1:])
['grocer', 'hardware', 'barber', 'clothes']
>>> (itinerary[:2] + ["barber"]) + itinerary[-1:]
['grocer', 'hardware', 'barber', 'clothes']
>>> 

Seems to be the same.
Nice...

But is it always so?
Or did it just happen by coincidence?

IOW: Does list-+ satisfy an associative LAW of the form:

(∀ a,b,c:list • (a + b) + c = a + (b + c) ) ??


Now try it with mutation.

Some (wiseguy) told me that mutating data structures can be more efficient
than 'functional' ones.

Sounds good... Just not sure how/when associativity works/breaks.

Say we want to stuff a 4 between the 2 and the 3 in [1,2,3]
[Its a bit tiresome to keep typing 'grocer' etc]

>>> it = [1,2,3]
>>> it[:2]
[1, 2]
>>> it[:2].append(4)
>>> it
[1, 2, 3]

Ho Hum.
Try again

>>> it = [1,2,3]
>>> it = it[:2].append(4)
>>> it = it.append(it[-1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'append'


Got to 'un-None' the #$@%%^ thing

>>> def app(a,b):
...   a.extend(b)
...   return a
... 

>>> a,b,c=[1,2],[3,4],[5,6]
>>> app(app(a,b),c)
[1, 2, 3, 4, 5, 6]
>>> a,b,c=[1,2],[3,4],[5,6]
>>> app(a,app(b,c))
[1, 2, 3, 4, 5, 6]

Looks right so far.
A few more checks...

>>> a,b=[1,2],[3,4]
>>> app(app(a,a),b)  # (a + a) + b
[1, 2, 1, 2, 3, 4]
>>> a,b=[1,2],[3,4]  # ok
>>> app(a,app(a,b))  # a + (a + b)
[1, 2, 3, 4, 1, 2, 3, 4]  # Whooops!


No I am not claiming that my pretence at noob struggles is very convincing.
Hopefully works enough to show that mutatory programming may be a lot of 'fun'
However imperative ≡ mutating  ≢ not-lawful

In Summary:

Correct programs are in accordance with math/logic laws
Incorrect programs are written by people ignorant of laws
Immutability is the bedrock of lawfulness -- If Newton had assumed
that between the time Kepler made his calculations and Newton recast them
into general form, the gravitional constant 'G' was changing, we would not
have physics or science as we know it
Properties like (python's version of) immutability just make stating of lawful
behavior unduly hard by asserting gibberish such as
[1,2,3] is [1,2,3,4].

[Just to be clear, I find python's notion of immutability sloppy but hard
to avoid whereas python's use of the 'is' operator for pointer-equality
a much bigger linguistic blunder. The two errors are closely related]

Piquant footnote: The big proponent of 'lawful¹' programming is
Lambert Meertens, who happens to be the grandfather² of python.

¹ http://www.kestrel.edu/home/people/meertens/publications/papers/Algorithmics.pdf
² http://en.wikipedia.org/wiki/ABC_%28programming_language%29

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can Python function return multiple data? fl <rxjwg98@gmail.com> - 2015-06-02 14:27 -0700
  Re: Can Python function return multiple data? Joel Goldstick <joel.goldstick@gmail.com> - 2015-06-02 17:35 -0400
  Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-02 14:40 -0700
  Re: Can Python function return multiple data? John Gordon <gordon@panix.com> - 2015-06-02 21:40 +0000
  Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-03 09:56 +1000
    Re: Can Python function return multiple data? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-06-03 15:56 +0200
      Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-04 07:35 +1000
      Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-03 22:56 +0100
        Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-03 15:28 -0700
          Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-03 21:30 -0400
          Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-04 11:52 +1000
          Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-04 23:47 +1000
            Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 17:25 +0300
              Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-04 14:37 +0000
                Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 18:04 +0300
                Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-04 19:51 -0400
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 03:11 +1000
                Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 20:30 +0300
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:37 +1000
                Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:30 -0400
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:31 +1000
                Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-04 18:38 +0000
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:52 +1000
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-05 09:04 +1000
                Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-05 02:02 +0000
                Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 09:11 +0200
                Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-05 12:27 +0300
                Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 14:04 +0200
                Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-04 21:52 +0100
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 09:13 +1000
                Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 01:16 +0100
                Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-05 02:40 +0100
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 11:48 +1000
                Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 11:06 +0100
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-05 07:44 +1000
                Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 10:51 +0100
            Re: Can Python function return multiple data? ElChino <elchino@cnn.cn> - 2015-06-04 17:37 +0200
              Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-04 19:57 -0400
            Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:26 -0400
              Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:16 +1000
                Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 18:59 -0400
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 12:37 +1000
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-04 20:16 -0700
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 21:06 +1000
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 06:29 -0700
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 07:59 +1000
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 20:20 -0700
                Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-05 23:28 -0400
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 22:28 -0700
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 15:43 +1000
                Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-07 08:49 -0700
                Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:07 +1000
                Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-07 09:20 -0700
                Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:34 +1000
                Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-20 18:59 -0700
                Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-21 12:32 +1000
                Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-20 19:50 -0700
                Re: Lawful != Mutable (was Can Python function return multiple data?) Laura Creighton <lac@openend.se> - 2015-06-21 11:14 +0200
                Re: Lawful != Mutable (was Can Python function return multiple data?) Ron Adam <ron3200@gmail.com> - 2015-06-21 08:55 -0400
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:51 +1000
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 13:49 +1000
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-06 14:50 +1000
                Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 15:29 +1000
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 22:32 -0700
                Re: Can Python function return multiple data? Dave Farrance <df@see.replyto.invalid> - 2015-06-06 07:52 +0100
                Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:53 +1000
                Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-05 08:21 -0400
                Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-05 16:37 +0300
                Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-04 19:07 -0700
            Re: Can Python function return multiple data? Michael Torrie <torriem@gmail.com> - 2015-06-04 11:36 -0600
            Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:51 -0400
            Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-04 20:17 +0200
              Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:45 +1000
                Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 08:59 +0200
          Re: Can Python function return multiple data? Michael Torrie <torriem@gmail.com> - 2015-06-04 08:16 -0600
        Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-05 11:55 -0700
      Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 01:01 -0400
      Re: Can Python function return multiple data? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-04 12:34 -0600
      Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-04 20:16 +0100
  Re: Can Python function return multiple data? Serhiy Storchaka <storchaka@gmail.com> - 2015-06-04 09:56 +0300
  Re: Can Python function return multiple data? fl <rxjwg98@gmail.com> - 2015-06-06 10:57 -0700
    Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-06 21:35 -0700
    Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:47 +1000
  Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:33 +1000

csiph-web