静态方法导致意外缩进错误?
Static method is causing an unexpected indent error?
出于某种原因,当我 运行 这段代码时,我在 @staticmethod 行出现意外的缩进错误,但我不知道为什么!一切都正确缩进,但这导致我的所有方法在代码 post 中引发错误。此外,我从 class 中删除了一些其他方法以缩短我在此处 post 的代码。
class Reservation:
"""A data type that represents a hotel room booking system."""
booking_numbers = []
@staticmethod
def get_reservations_from_row(room_obj, list_tuples):
reservation_dict = {}
booking_num_list = []
date_list = []
for element in list_tuples:
year, month, day, short_str = element
list_short_str = short_str.split('--')
booking_num = int(list_short_str[0])
name = list_short_str[1]
if booking_num not in booking_num_list:
booking_num_list.append(booking_num)
date = datetime.date(year, month, day)
date_list.append(date)
for element in booking_num_list:
print(date_list)
date_list.sort()
value = Reservation(name, room_obj, date_list[0], date_list[len(date_list)-1], element)
reservation_dict[element] = reservation_dict.get(element, []) + value
return reservation_dict
回溯(最后一次调用):
文件“/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py”,第 35 行,在解析中
return 编译(源代码、文件名、模式、PyCF_ONLY_AST)
文件“/Users/myname/Downloads/reservation.py”,第 74 行
@静态方法
^
IndentationError:意外缩进
看来你用了tab
来缩进。
删除制表符并插入 4
个适合您的空格
出于某种原因,当我 运行 这段代码时,我在 @staticmethod 行出现意外的缩进错误,但我不知道为什么!一切都正确缩进,但这导致我的所有方法在代码 post 中引发错误。此外,我从 class 中删除了一些其他方法以缩短我在此处 post 的代码。
class Reservation:
"""A data type that represents a hotel room booking system."""
booking_numbers = []
@staticmethod
def get_reservations_from_row(room_obj, list_tuples):
reservation_dict = {}
booking_num_list = []
date_list = []
for element in list_tuples:
year, month, day, short_str = element
list_short_str = short_str.split('--')
booking_num = int(list_short_str[0])
name = list_short_str[1]
if booking_num not in booking_num_list:
booking_num_list.append(booking_num)
date = datetime.date(year, month, day)
date_list.append(date)
for element in booking_num_list:
print(date_list)
date_list.sort()
value = Reservation(name, room_obj, date_list[0], date_list[len(date_list)-1], element)
reservation_dict[element] = reservation_dict.get(element, []) + value
return reservation_dict
回溯(最后一次调用): 文件“/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py”,第 35 行,在解析中 return 编译(源代码、文件名、模式、PyCF_ONLY_AST) 文件“/Users/myname/Downloads/reservation.py”,第 74 行 @静态方法 ^ IndentationError:意外缩进
看来你用了tab
来缩进。
删除制表符并插入 4
个适合您的空格