Object of type 'builtin_function_or_method' has no len()

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

Object of type 'builtin_function_or_method' has no len() #

The Python "TypeError: object of type 'builtin_function_or_method' has no len()" occurs when we pass a method or a function without calling it to the len() function. To solve the error, make sure to call the method with parenthesis, e.g. my_method().

typeerror object of type builtin function or method has no len

Here is an example of how the error occurs.

main.py
message = input # ⛔️ TypeError: object of type 'builtin_function_or_method' has no len() print(len(message))

We passed the input built-in method to the len() function which caused the error.

To solve the error, we have to call the method and pass the result to the len() function.

main.py
# 👇️ call method with parens, e.g. input() message = input() print(len(message))

We used parentheses to invoke the method, so the len() function now gets called with a string and not a built-in method object.

If your built-in method takes arguments, make sure to pass them when calling it, e.g. my_method(10, 20).

The len() function returns the length (the number of items) of an object.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = len(my_list) print(result) # 👉️ 3
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_list = ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True print(type(input)) # 👉️ <class 'builtin_function_or_method'> print(callable(input)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

The callable function takes an object as an argument and returns True if the object appears callable, otherwise False is returned.

If the callable() function returns True, it is still possible that calling the object fails, however if it returns False, calling the object will never succeed.

Conclusion #

The Python "TypeError: object of type 'builtin_function_or_method' has no len()" occurs when we pass a method or a function without calling it to the len() function. To solve the error, make sure to call the method with parenthesis, e.g. my_method().

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.