Absolute in terms of being positive or zero, and never negative. The return will be an integer of the physical element count of the iterable. Only iterable objects can have a length.
Aside
When a variable points to a singular object, then a singular term would be more apt than a plural one. strings
implies more than one string, such as a list of strings.
strings = [ 'one', 'two', 'three', 'four' ]
for string in strings:
pass
So for the above, a reader might make more sense of the signature line if it is written like so,
def get_length(string):
pass
I would also not pass in the counter variable, but rather declare it inside the function so it always starts at zero.
def get_length(string):
counter = 0
for char in string:
pass
Again, the name we give a variable is arbitrary but it helps if it conveys meaning to the reader. Above I chose char
as my variable since while all letters are characters, not all characters are letters. The meaning of ‘char’ conveys generally, where as ‘letter’ conveys specifically. Semantics, eh? Not deal breaking, but ever useful to the reader when we think in those terms.
Okay, so what we have here is a function to count the number of elements in an iterable, be that a string, a list, a tuple, a set, even a dictionary (key count). Taking this into account, we can rewrite our function to convey that any iterable can be accepted.
>>> def get_length(iterable):
... if not hasattr(iterable, '__iter__'):
... raise ValueError
... count = 0
... for element in iterable:
... count += 1
... return count
...
>>> get_length(36535)
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
get_length(36535)
File "<pyshell#55>", line 3, in get_length
raise ValueError
ValueError
>>> get_length("The way I understand this is that the len() function returns an absolute value ")
79
>>> get_length({1: 'a', 2: 'b'})
2
>>> get_length([5, 4, 3, 6, 7, 8, 1, 2, 9])
9
>>> get_length((2, 4, 6, 8, 10))
5
>>> get_length({6, 2, 8, 9, 1, 4})
6
>>> get_length(True)
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
get_length(True)
File "<pyshell#55>", line 3, in get_length
raise ValueError
ValueError
>>>
This can go on the back burner, for now, as I suspect you will not have covered everything in the code. but you should be able to get the gist.