How can I count the number of elements in an array, because contrary to logic array.count(string) does not count all the elements in the array, it just searches for the number of occurrences of string.
A:
Or,
myArray.__len__()
if you want to be oopy; "len(myArray)" is a lot easier to type! :)
Kevin Little
2008-10-09 14:23:27
Race conditions! Yuck.
Gregg Lind
2008-10-09 14:25:35
Thats a definite Yuk.
Unkwntech
2008-10-09 14:32:51
@Gregg Lind, This does not introduce a race condition. (It is, however, as yucky as it gets.)
Mike Graham
2010-03-19 07:29:16
The race condition was between me and another poster! Note my *even more downvoted answer* below!
Gregg Lind
2010-03-19 21:25:12
(I realize now, that my answer is deleted, but essentially Kevin Little and I said exactly the same thing at the same time).
Gregg Lind
2010-03-19 21:26:19
+5
A:
len
is a built-in function that calls the given container object's __len__
member function to get the number of elements in the object.
Functions encased with double underscores are usually "special methods" implementing one of the standard interfaces in Python (container, number, etc). Special methods are used via syntactic sugar (object creation, container indexing and slicing, attribute access, built-in functions, etc.).
Using obj.__len__()
wouldn't be the correct way of using the special method, but I don't see why the others were modded down so much.
Jeremy Brown
2008-10-09 19:40:04
Especially when both of us mentioned that is was bad form. Understanding what length "really" does is important in it's own right.
Gregg Lind
2008-10-14 17:11:25