after del list , when I use it again, prompt 'not defined'.how could i delete its element,but not itself?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Jun 2 20:21:37 EDT 2006


python a écrit :
> after del list , when I use it again, prompt 'not defined'.how could i
> delete its element,but not itself?
> except list.remove(val) ,are there  other ways to delete list 's
> elements?
> 
> and , I wrote:
> 
> list1=[]
> def method1():

Why naming a function "method" ?

>      global list1

globals are evil.

>      list1=['a','b','c','d']
> def method2():
>      list2=list1

this makes list2 point to the same object as list1.

>      list2.remove['a']
> main():
>     global list1
>      method1()
>      method2()
>      print list1[0]
> 
> it  prints 'b'.

Of course. Python's "variable" are really just names referencing 
objects. Making two names pointing to the same object does'nt make two 
different objects, just two references to one object.

> 
> How could I keep the list1 not to change when remove list2's elements?

Copy the list.





More information about the Python-list mailing list