list.clear() missing?!?

Peter Hansen peter at engcorp.com
Thu Apr 13 07:41:28 EDT 2006


Alan Morgan wrote:
> Ah, but if you know your basic python then you wouldn't be looking for
> s.clear() in the first place; you'd just use s[:]=[] (or s=[], whichever
> is appropriate).  

One of very first things newcomers learn (I believe, though I don't know 
how soon the tutorial teaches it) is to use "dir()" on objects.  The 
clear() method would show up there and quickly attract attention. 
Neither of the other techniques are likely to be discovered as quickly. 
  (Well, okay, s=[] would be, I'm sure, but to many newcomers that might 
"feel wrong" as the way to empty out a list, but then we're back to 
wondering how often there's really a usecase for doing so.)

> Personally, it seems more *reasonable* to me, a novice python user,
> for s.clear() to behave like s=[] (or, perhaps, for s=[] and s[:]=[] to
> mean the same thing).  The fact that it can't might be an argument for
> not having it in the first place.

Then it's a good reason we had this thread, so you could learn something 
*crucial* to understanding Python and writing non-buggy code: name 
binding versus variables which occupy fixed memory locations like in 
some other languages.  This has to be by far the most frequent area that 
newcomer's trip up.  But that's another story...

-Peter




More information about the Python-list mailing list