Leo wrote: > Steven D'Aprano wrote: > >> On Mon, 20 Apr 2009 15:13:15 -0500, Grant Edwards wrote: >> >> >>> I fail to see the difference between "length greater than 0" and "list >>> is not empty". They are, by definition, the same thing, aren't they? >> >> For built-in lists, but not necessarily for arbitrary list-like >> sequences. >> >> There's also the performance issue. I might have a sequence-like >> structure where calling len() takes O(N**2) time, (say, a graph) but >> calling __nozero__ might be O(1). Why defeat the class designer's >> optimizations? > Is that true? > Calling len() actually traverses the whole list?
No. Steven constructed the theoretical case where determining whether a sequence (not Python's list) is empty is a cheap operation while getting the actual length is costly. The only "popular" data structure I know that implements its length like this are strings in C. Peter -- http://mail.python.org/mailman/listinfo/python-list