【问题标题】:Static method is causing an unexpected indent error? [duplicate]静态方法导致意外缩进错误? [复制]
【发布时间】:2021-04-29 04:54:20
【问题描述】:

由于某种原因,当我运行此代码时,@staticmethod 行出现意外缩进错误,但我不知道为什么!一切都正确缩进,但这导致我的所有方法在代码发布中引发错误。此外,我从类中删除了一些其他方法以缩短我在此处发布的代码。

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

Traceback(最近一次调用最后一次): 解析中的文件“/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py”,第 35 行 返回编译(源,文件名,模式,PyCF_ONLY_AST) 文件“/Users/myname/Downloads/reservation.py”,第 74 行 @静态方法 ^ IndentationError: 意外缩进

【问题讨论】:

    标签: python python-3.x error-handling indentation


    【解决方案1】:

    您似乎使用了tab 进行缩进。 删除选项卡并插入 4 空格它将为您工作

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 2021-05-28
      • 1970-01-01
      • 2016-04-30
      • 2021-01-14
      • 2011-05-14
      • 2020-12-17
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多