Reputation: 27
Please let me know the reason for the error in the code below.
def lenli(ab):
results=[]
for a in ab:
results.append(len(a))
return results
shows an error
TypeError Traceback (most recent call last)
<ipython-input-69-8e23c7ef98a0> in <module>()
----> 1 lenli[ab]
TypeError: 'function' object has no attribute '__getitem__'
thanks sindia
Upvotes: 0
Views: 98
Reputation: 599876
The problem is not in the code you've shown, but in the code that calls it. Functions are called with parentheses, ()
, not square brackets.
Upvotes: 1
Reputation: 78770
You are calling your function wrong. Square brackets []
are the shorthand for __getitem__
(which lenli
does not have). Parentheses ()
are the shorthand for __call__
, which you want.
In short: type lenli(ab)
.
Upvotes: 1