Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: TypeError: object of type 'NoneType' has no len()

Tags:

python

I am getting an error from this Python code:

with open('names') as f:
    names = f.read()
    names = names.split('\n')
    names.pop(len(names) - 1)
    names = shuffle(names)
    f.close()

assert len(names) > 100

Error:

Python: TypeError: object of type 'NoneType' has no len()

The assert statement is throwing this error, what am I doing wrong?

like image 665
mavix Avatar asked Jul 06 '12 03:07

mavix


People also ask

How do I fix NoneType in Python?

The Python TypeError: NoneType Object Is Not Iterable error can be avoided by checking if a value is None or not before iterating over it. This can help ensure that only objects that have a value are iterated over, which avoids the error.

Has no len() Python?

The Python "TypeError: object of type 'function' has no len()" occurs when we pass a function without calling it to the len() function. To solve the error, make sure to call the function and pass the result to the len() function.

How do you fix TypeError argument of type NoneType is not iterable?

The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None .

What is a NoneType in Python?

NoneType in Python is a data type that simply shows that an object has no value/has a value of None . You can assign the value of None to a variable but there are also methods that return None .


1 Answers

shuffle(names) is an in-place operation. Drop the assignment.

This function returns None and that's why you have the error:

TypeError: object of type 'NoneType' has no len()
like image 179
JBernardo Avatar answered Oct 01 '22 17:10

JBernardo