错误:在函数外部'返回'

时间:2015-04-04 02:48:24

标签: python python-2.7

错误:在函数外部'返回'

为什么?

 def get_all_students(self):
    database = open(self.database_path, 'r')
    list_of_students = list()
    for idx, l in enumerate(database.readlines()):
        params = list()
        params.append(idx)
        params += l.split(self.data_delimiter)
        student = Student(*self.item_un_escape(params))
        list_of_students.append(student)
    return list_of_students

编辑:我会发布课程的其余部分,我很确定它是正确缩进的。似乎无法找到错误,我敢打赌它可能是愚蠢的东西。道歉,第一次在python编码,但这个错误让我烦恼了一个小时左右!哈哈

Edit2:在params.append(idx)上发出意外缩进的错误?

from student import Student

database_path = 'C:/Users/Alan/Desktop/flask/flask/app/database'


class Database(object):
    data_delimiter = ','

    @staticmethod
    def escape_new_lines(value):
        if type(value) == str:
            value = value.replace('\n', '@n-nl@')
            value = value.replace('\r', '@r-nl@')
        return value

    @staticmethod
    def un_escape_new_lines(value):
        if type(value) == str:
            value = value.replace('@n-nl@', '\n')
            value = value.replace('@r-nl@', '\r')
        return value

    def __init__(self, database_path=database_path):
        self.database_path = database_path

    def get_all_students(self):
        database = open(self.database_path, 'r')
        list_of_students = list()
        for idx, l in enumerate(database.readlines()):
            params = list()
            params.append(idx)
            params += l.split(self.data_delimiter)
            student = Student(*self.item_un_escape(params))
            list_of_students.append(student)
        return list_of_students

1 个答案:

答案 0 :(得分:3)

您发布的代码很好。您可能的错误是在返回语句之前缺少缩进。 编辑:正如@Anonymous在评论中指出的那样,另一种可能性是你将制表符缩进与空格缩进混合。检查你的缩进。