我寫了下面的類和程序:Python中如何應用類正確
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行seprated通過=
。我想用這樣的下一行編織每行:
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
這是怎麼回事?
問題是你不返回任何東西從'split_and_add'因此行是None – user2097159 2014-12-02 15:11:44
我想''yield'process_row(line)'在'split_and_add'函數中。否則,你正在返回'無' – GWW 2014-12-02 15:11:52
我怎麼能解決這個問題? – Pieter 2014-12-02 15:14:47