TypeError: object of type ‘builtin_function_or_method’ has no len()

The python error TypeError: object of type ‘builtin_function_or_method’ has no len() occurs while attempting to find the length of an object that returns a build in function or a method. The python variables can have a reference of a build in function or a method. If a python variable refers a builtin_function_or_method, it can’t be used in length function. The Length function is used for data structures that store multiple objects. 

The python variables can store the build in functions and user defined methods. These variables are not assigned any value, or objects. The length function can not be called the variables which are not allocated with any value or object. If you call the length function for these variables of ‘builtin_function_or_method’, it will throw the error TypeError: object of type ‘builtin_function_or_method’ has no len()



Example

For example, ‘len’ is a built in function in python, len() returns an integer value that calculates the length of the object. Python supports to store a build in function or method. The build in function or method can be executed to find the return value.

a = len             - reference the build in function 'len'
a = len( [1,2])     - returns an integer value from len build in function


Exception

The error TypeError: object of type ‘builtin_function_or_method’ has no len() will show the stack trace as below

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print len(s)
TypeError: object of type 'builtin_function_or_method' has no len()
[Finished in 0.0s with exit code 1]


How to reproduce this error

Create a python variable which is assigned with a built in function or method. If the length function is invoked for this variable, the error TypeError: object of type ‘builtin_function_or_method’ has no len()  will be thrown.

s=max
print len(s)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print len(s)
TypeError: object of type 'builtin_function_or_method' has no len()
[Finished in 0.0s with exit code 1]


Root Cause

Python has many build in functions. These functions are used to accomplish a task to be performed with in the code. Also the python supports the development of user-defined functions called methods. The programmer will create a custom function to execute the code that can be used again.

In Addition, python can reference these function in a python variable. These python variables are either build in function data type or user defined method data type. These objects can’t use in length function to find the length. If these variables are passed in the length function then the error TypeError: object of type ‘builtin_function_or_method’ has no len() will be thrown



Solution 1

The python variable which is assigned with a build in function or method, should be assigned with a value or object. If the variable is not assigned with any value or object , assign with the respective build in function. So the build in function will be called to perform operation.

s=max(1,3)
print s

Output

3
[Finished in 0.0s]


Solution 2

The python variable which is assigned with a build in function or method, should be assigned with a value or object. If the variable is not assigned with any value or object , assign with an object such as list, tuple, set, dictionary etc.

s=( max(1,3), min(1,3) )
print s
print len(s)

Output

(3, 1)
2
[Finished in 0.0s]


Solution 3

Due to the dynamic creation of the variable the python variable may assigned with built in function or method. The datatype of the variable is ‘function’ or ‘method’. In this case, the function must be checked before the length function is called.

x = [1,2]
s=len
print type(s)

if callable(s) : 
	print (s( x ))
else:
	print "Value is not callable"

Output

<type 'builtin_function_or_method'>
2
[Finished in 0.0s]


Solution 4

The try and except block is used to capture the unusual run time errors. If the python variable contains expected value, then it will execute in the try block. If the python variable contains the unexpected value, then the except block will handle the error.

x = [1,2]
s=len
print type(s)

try : 
	print (s( x ))
except :
	print "Value is not callable"

Output

<type 'builtin_function_or_method'>
2
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *