A question on modification of a list via a function invocation

Rustom Mody rustompmody at gmail.com
Mon Sep 4 11:17:25 EDT 2017


On Monday, September 4, 2017 at 8:37:45 PM UTC+5:30, Steve D'Aprano wrote:
> On Tue, 5 Sep 2017 12:34 am, Rustom Mody wrote:
> 
> > On Monday, September 4, 2017 at 5:58:18 PM UTC+5:30, ROGER GRAYDON CHRISTMAN
> > wrote:
> >> Or with just one function: >>> def baz(x,y):
> >>  x += y
> >> >>> a = 10
> >> >>> b = [10]
> >> >>> baz(a,a)
> >> >>> a
> >> 10
> >> >>> baz(b,b)
> >> >>> b[10, 10]
> > 
> > Ha Ha!  Lovely
> > 
> > [I was about to ask Chris if he is being serious about relating this to the
> > presence of '=' ]
> > 
> > However if you desugar the += into __iadd__ then someone may argue
> > the presence of the = is an optical illusion
> 
> I'm afraid I can't quite follow what people think this example actually
> demonstrates.

Anton gave a picture explaining why/how references are needed and to be understood

I pointed out that while that picture is fine (and necessary) as part
of the explanation, it does not properly address the central question
of this thread, viz How to understand parameter passing in python.
With this example:

>>> def foo(x): x += 2
>>> def bar(x): x.append(2)
>>> a=10
>>> b=[10]
>>> foo(a)
>>> a
10
>>> bar(b)
>>> b
[10, 2]
>>> 

Roger Christman compressed my foo and bar into one baz

def baz(x,y):
   x += y

Which leaks or doesnt leak x-modifications depending on its type



More information about the Python-list mailing list