how to force a list to be local?

Robert Brewer fumanchu at amor.org
Thu Aug 5 19:57:58 EDT 2004


Mark Harrison wrote:
> In the following code, t1 does not modify b, while t2 seems to
> modify b.  What do I need to do in order to make b local in t2
> so that b.remove() does not modify the passed in list?
> 
> def t1(a,b):
>     b = []
> 
> def t2(a,b):
>     b.remove(4)

Short answer: make a copy of the list.

def t2(a, b):
    b = b[:]
    b.remove(4)


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list