Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is a class:

class CoordinateRow(object):

def __init__(self):
    self.coordinate_row = []

def add(self, input):  
    self.coordinate_row.append(input)

def weave(self, other):
    result = CoordinateRow()
    length = len(self.coordinate_row)
    for i in range(min(length, len(other))):
        result.add(self.coordinate_row[i])
        result.add(other.coordinate_row[i])
    return result

This is a part of my program:

def verwerk_regel(regel):
cr = CoordinateRow()
coordinaten = regel.split()
for coordinaat in coordinaten:
    verwerkt_coordinaat = verwerk_coordinaat(coordinaat)
    cr.add(verwerkt_coordinaat)
cr2 = CoordinateRow()
cr12 = cr.weave(cr2)
print cr12

def verwerk_coordinaat(coordinaat):
coordinaat = coordinaat.split(",")
x = coordinaat[0]
y = coordinaat[1]
nieuw_coordinaat = Coordinate(x)
adjusted_x = nieuw_coordinaat.pas_x_aan()
return str(adjusted_x) + ',' + str(y)

But I'm geting an error at "cr12 = cr.weave(cr2)":

for i in range(min(length, len(other))):

TypeError: object of type 'CoordinateRow' has no len()

like image 479
user2971812 Avatar asked Nov 23 '14 13:11

user2971812


People also ask

What does TypeError object of type int has no len () mean?

The Python "TypeError: object of type 'int' has no len()" occurs when we pass an integer to the len() function. To solve the error, convert the integer to a string, e.g. len(str(my_int)) or correct the assignment and pass a sequence ( list , str , etc) to the len() function.

How to fix object of type NoneType has no len()?

The Python "TypeError: object of type 'NoneType' has no len()" occurs when we pass a None value to the len() function. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

What is __ len __ in Python?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

How do you use LEN in Python?

Basic Syntax for len() in Python To use the len() function to get the length of a data type, assign the data type to a variable, then pass the variable name to the len() function.


1 Answers

You need to add a __len__ method, then you can use len(self) and len(other):

class CoordinateRow(object):
    def __init__(self):
        self.coordinate_row = []

    def add(self, input):
        self.coordinate_row.append(input)

    def __len__(self):
        return len(self.coordinate_row)

    def weave(self, other):
        result = CoordinateRow()
        for i in range(min(len(self), len(other))):
            result.add(self.coordinate_row[i])
            result.add(other.coordinate_row[i])
        return result
In [10]: c = CoordinateRow()    
In [11]: c.coordinate_row += [1,2,3,4,5]    
In [12]: otherc = CoordinateRow()    
In [13]: otherc.coordinate_row += [4,5,6,7]    
In [14]:c.weave(otherc).coordinate_row
[1, 4, 2, 5, 3, 6, 4, 7]
like image 186
Padraic Cunningham Avatar answered Sep 26 '22 15:09

Padraic Cunningham