[Tutor] Static Variable in Functions

ALAN GAULD alan.gauld at btinternet.com
Mon Mar 14 11:15:16 CET 2011


Apologies to all, I didn't notice the [] around a.

But the basic point remains that the list that he is appending 
to is the same list as b refers to, albeit indirectly. He is still 
"touching" the list. Although not directly modifying the list 
to which b refers, since it still only holds one member, the 
list to which a refers....

 


Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/




----- Original Message ----
> From: Andre Engels <andreengels at gmail.com>
> To: Alan Gauld <alan.gauld at btinternet.com>
> Cc: tutor at python.org
> Sent: Monday, 14 March, 2011 9:23:47
> Subject: Re: [Tutor] Static Variable in Functions
> 
> On Mon, Mar 14, 2011 at 9:56 AM, Alan Gauld <alan.gauld at btinternet.com>  
wrote:
> > "Yasar Arabaci" <yasar11732 at gmail.com>  wrote
> >
> >> >>> a=["a"]
> >> >>>  b=[a]
> >> >>> a.append("c")
> >> >>>  b
> >> [['a', 'c']]
> >>
> >> Apperantly, I can change  something (which is mutable) inside  a list
> >> without even touching  the list itself :)
> >
> > But the point is that you *are* touching the  list.
> > In this case you have two names referring to the same  list.
> > You can modify that list (because it is mutable) via either name,  it
> > makes no difference because they both refer to the same  list.
> >
> > So a.append() is exactly the same operation as  b.append()
> 
> No, they are not the same list. b is (a name of) a list with  one
> element, that one element being the list (denoted by) a. That's  not
> the same as a itself.
> 
> -- 
> André Engels, andreengels at gmail.com
> 


More information about the Tutor mailing list