PEP8 和来自导入模块的长方法名称

PEP8 and long method names from imported modules

我似乎找不到关于此的问题,但由于导入了一个模块,我在维护 PEP8 时遇到了问题。

我正在使用 TextGridTools (tgt) 模块来解析 TextGrid 文件,这是一种用于注释语音文件的格式。问题是它的函数名称很长,例如 get_annotations_between_timepoints.

因为我在带有循环、条件和列表理解的 class 方法中使用它,所以它已经明显缩进了:

def align_intervals(self):
  print('Aligning intervals...')
  brk = self.brk_intervals
  all_atts = self.all_attributes

  word_list = []
    for i in range(len(brk)):
      if i == 0:
        word_list.append([att.get_annotations_between_timepoints(0, brk[0].time) for att in all_atts])
      else:
        word_list.append([att.get_annotations_between_timepoints(brk[i-1].time, brk[i].time) for att in all_atts])
  return word_list

有什么建议吗?

您可以断开括号之间的线而不会产生不良影响。事实上,官方 PEP-8 文档也这么说:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

def align_intervals(self):
    print('Aligning intervals...')
    brk = self.brk_intervals
    all_atts = self.all_attributes

    word_list = []
    for i in range(len(brk)):
        if i == 0:
            word_list.append([
                att.get_annotations_between_timepoints(
                    0, brk[0].time
                ) for att in all_atts
            ])
        else:
            word_list.append([
                att.get_annotations_between_timepoints(
                    brk[i - 1].time, brk[i].time
                ) for att in all_atts
            ])
    return word_list

另一种方法是使用较短的局部变量为长函数起别名:

get_tpts = att.get_annotations_between_timepoints

然后在需要时使用该别名。

是的,只需插入一些换行符:

def align_intervals(self):
    print('Aligning intervals...')
    brk = self.brk_intervals
    all_atts = self.all_attributes

    word_list = []
        for i in range(len(brk)):
        if i == 0:
            word_list.append(
                [
                    att.get_annotations_between_timepoints(0, brk[0].time) 
                    for att in all_atts
                ]
            )
      else:
        word_list.append(
            [
                att.get_annotations_between_timepoints(brk[i-1].time, brk[i].time) 
                for att in all_atts
            ]
        )
  return word_list

代码风格没有绝对的答案,毕竟是主观的东西。 Black 最近推出了一个非常好的代码格式化程序 IMO。但最后,做你/你的团队最喜欢的事。

您可以自己给长方法名起一个短名称:

import package

short_method = package.ungodly_ridiculous_long_name_for_a_method
short_class = package.another_ungodly_unnecessarily_long_name_for_a_class

几个选项:

  • 定义别名,如 Jonah Bishop 所建议的那样
  • 重构代码并创建将 return 列表
  • 的函数
  • 重构代码以降低缩进级别

所以你可以替换

word_list = []
    for i in range(len(brk)):
        if i == 0:
            word_list.append([
                att.get_annotations_between_timepoints(
                    0, brk[0].time
                ) for att in all_atts
            ])
        else:
            word_list.append([
                att.get_annotations_between_timepoints(
                    brk[i - 1].time, brk[i].time
                ) for att in all_atts
            ])

    def gabt(brk0, brk1, all_atts):
        att_gabt = att.get_annotations_between_timepoints
        return [att_gabt(brk0.time, brk1.time) for att in all_atts]

    word_list = [gabt(0, brk[0].time, all_atts)]
    for brk0, brk1 in zip(brk[:-1], brk[1:]):
        word_list.append(gabt(brk0.time, brk1.time, all_atts))

因为它在 class 中,重构后的代码可能如下所示:

def _gabt(self, brk0, brk1):
    att_gabt = att.get_annotations_between_timepoints
    return [att_gabt(brk0.time, brk1.time) for att in self.all_attributes]

word_list = [self._gabt(0, brk[0].time)]
for brk0, brk1 in zip(brk[:-1], brk[1:]):
    word_list.append(self._gabt(brk0.time, brk1.time))