views:

139

answers:

2

If the my_list variable is global, you can't do:

my_list = []

that just create a new reference in the local scope.

Also, I found disgusting using the global keyword, so how can I empty a list using its methods?

+1  A: 

How about the following to delete all list items:

def emptyit():
    del l[:]
Joe Koberg
Replacing a practically atomic statement with a function?
Beau Martínez
@Beau, it ain't atomic at all. I wouldn't say that 'practically' even comes close.
Aaron Gallagher
@Aaron You're right... Emptying a list is about as unatomic as you can get. I meant semantically atomic, by which I mean it's read as just one statement in the language (delete all of this list).
Beau Martínez
@Beau: The function creates a local scope, that seemed pretty obvious that's what he was doing from the question... He doesn't use the global keyword, and if he wasn't in a function it would just modify the in-scope variable, not the local copy. ....
Joe Koberg
+10  A: 
del a[:]

or

a[:] = []
Daniel Stutzbach