Question about idioms for clearing a list
bonono at gmail.com
bonono at gmail.com
Fri Feb 10 05:22:52 EST 2006
Magnus Lycka wrote:
> >>> class BryansList(list):
> ... add=list.append
> ... def clear(self):
> ... del self[:]
> ...
> >>> b = BryansList([1,2,3,4,5])
> >>> b
> [1, 2, 3, 4, 5]
> >>> b.add(6)
> >>> b.clear()
> >>> b
> []
>
> Happy now? You can keep it, I don't need it. :)
> Most of us consider minimal interfaces a virtue.
What kind of performance penalty are we talking about here ? list being
such a fundamental thing, no one would like to use a slower version
just for the clear/add method. And if it is a "use when you really need
to", it would make the code harder to understand as it would be
"sometimes it is BryansList, sometimes it is builtin list".
That said, I don't find clear() to be useful as unless one needs to
pass around a single copy of list object around and saved them for
future use(which can be a source of subtle bug), just lst=[] is usually
good enough for localized usage.
More information about the Python-list
mailing list