Python 错误:';返回';功能外

Python 错误:';返回';功能外,python,python-2.7,Python,Python 2.7,错误:函数外部的“return” 为什么 编辑:我将发布这个类的其余部分,我很确定它的缩进是正确的。似乎找不到错误,我敢打赌这可能是愚蠢的。抱歉,这是我第一次用python编写代码,但这个错误已经困扰了我大约一个小时了!哈哈 Edit2:它在params.append(idx)上抛出一个错误,表示意外缩进 from student import Student database_path = 'C:/Users/Alan/Desktop/flask/flask/app/database'

错误:函数外部的“return”

为什么

编辑:我将发布这个类的其余部分,我很确定它的缩进是正确的。似乎找不到错误,我敢打赌这可能是愚蠢的。抱歉,这是我第一次用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

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

您发布的代码没有问题。可能的错误是在return语句之前缺少缩进编辑:正如@Anonymous在评论中指出的,另一种可能是您将制表符缩进与空格缩进混合。检查您的缩进。

您发布的代码没有问题。可能的错误是在return语句之前缺少缩进编辑:正如@Anonymous在评论中指出的,另一种可能是您将制表符缩进与空格缩进混合。检查您的缩进。

您发布的代码没有问题。可能的错误是在return语句之前缺少缩进编辑:正如@Anonymous在评论中指出的,另一种可能是您将制表符缩进与空格缩进混合。检查缩进。

您的代码可能混合了制表符和空格缩进。这就是问题所在。啊哈。新手失误!谢谢,您的代码可能混合了制表符和空格缩进。这就是问题所在。啊哈。新手失误!谢谢,您的代码可能混合了制表符和空格缩进。这就是问题所在。啊哈。新手失误!谢谢,您的代码可能混合了制表符和空格缩进。这就是问题所在。啊哈。新手失误!谢谢
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