Python如何正确应用类

时间:2014-12-02 15:08:56

标签: python class

我写了下面的课程和程序:

class Coordinate(object):
    def __init__(self,x,y):
        self.x_coordinate = x
        self.y_coordinate = y

class CoordinatRow(object):
    def __init__(self):
        self.coordinaterow = []

    def add_coordinaterow(self, coordinate):
        self.coordinaterow.append(coordinate)

    def weave(self,other):
        lijst = []
        for i in range(len(self.coordinaterow)):
            lijst.append(self.coordinaterow[i])
            lijst.append(other.row[i]) 
        coordinaterow = lijst
        print coordinaterow

程序:

from Coordinates import Coordinate, CoordinatRow
from ipy_lib import file_input

'''functies'''

def split_and_add(invoer):
    rij = invoer.split('=')
    for line in rij:
        process_row(line)  

def process_row(line):
    temp_coordinate_row = CoordinatRow()
    rij = line.split()
    for coordinate in rij:
        coor = process_coordinate(coordinate)
        temp_coordinate_row.add_coordinaterow(coor)
    return temp_coordinate_row

def process_coordinate(coordinate):
    idunno = coordinate.split(',')
    return Coordinate(int(idunno[0]),int(idunno[1]))

'''programma'''
bestand = file_input()
split_and_add(bestand)
rows = split_and_add(bestand)
for row in rows:
    row.weave(row)

输入如下:

5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4

Ech行由=分隔。我想用下一行编织每一行:

5.4, 4.5, 8.7 weave with 6.3, 3.2, 9.6, 4.3
5.4, 6.3, 4.5, 3.2, 8.7, 9.6, 4.3

以及此新行的下一行。

问题在于我认为我应用课程的方式。我收到了这个错误:

for row in rows:
TypeError: 'NoneType' object is not iterable

这里出了什么问题?

2 个答案:

答案 0 :(得分:3)

def split_and_add(invoer):
    rij = invoer.split('=')
    for line in rij:
        yield process_row(line) 

答案 1 :(得分:2)

如果您不想使用收益率

def split_and_add(invoer):
    rij = invoer.split('=')
    rows = []
    for line in rij:
        rows.append(process_row(line))
    return rows

对于你的第二个问题,你可能想要做这样的事情:

for row in range(0,len(rows)-1):
    rows[row].weave(rows[row+1])