在Python中打破嵌套函数/构造函数调用的正确方法是什么?

时间:2022-12-05 14:30:07

According to PEP 8:

根据PEP 8:

When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

使用悬挂式缩进时,应采用以下注意事项;第一行应该没有参数,应该使用进一步的缩进来明确区分自己作为延续线。

Suppose I have something like:

假设我有类似的东西:

my_object = VeryLongClassName(long_function_name(arg1, arg2), arg3)

which goes over 79 characters. Should I break like this:

超过79个字符。我应该像这样打破:

my_object = VeryLongClassName(
        long_function_name(arg1, arg2), arg3)

or this?

my_object = VeryLongClassName(long_function_name(
        arg1, arg2), arg3)

1 个解决方案

#1


2  

I use the following approach, which scales nicely across a wide variety of situations and tends to keeps lines short -- and thus makes the code easier to scan visually.

我使用以下方法,可以在各种情况下很好地扩展,并且可以保持线条短 - 从而使代码更容易在视觉上扫描。

my_object = VeryLongClassName(
    long_function_name(arg1, arg2),
    arg3,
)

There are a few added benefit of that approach:

这种方法有一些额外的好处:

  • It is widely used when defining large data structures (lists, dicts, and even JSON). It's handy to use a coding style that mimics your data layout style. Code is just another form of data, right?

    它在定义大型数据结构(列表,dicts甚至JSON)时被广泛使用。使用模仿数据布局样式的编码样式非常方便。代码只是另一种形式的数据,对吧?

  • It works great with most text editors, which approach the world from a line-oriented perspective. If each argument to a function or constructor is on a separate line, code refactoring is easy.

    它适用于大多数文本编辑器,它们从面向行的角度来看世界。如果函数或构造函数的每个参数都在一个单独的行上,代码重构很容易。

  • Its application is rule-based and purely mechanical. I never have to puzzle over how to indent the code.

    它的应用是基于规则和纯机械的。我永远不会想知道如何缩进代码。

  • As a result, it looks tidy and the governing principles are immediately clear. As a point of contrast, the indenting examples seen in PEP 8 look like a hodgepodge to my eye and thus don't provide very clear guidance.

    结果,它看起来很整洁,管理原则立即明确。作为对比点,在PEP 8中看到的缩进示例看起来像是我眼中的大杂烩,因此无法提供非常明确的指导。

Another strategy is to use local convenience variables, especially in situations where you need to use long names several times within a method. Although creating short mnemonics runs a risk of making code more obscure, it often helps with readability, provided that your code is already organized in fairly small functions or methods -- again because it tends to enhance the ease of visually scanning of the code.

另一种策略是使用局部便利变量,尤其是在需要在方法中多次使用长名称的情况下。尽管创建短助记符会使代码更加模糊,但它通常有助于提高可读性,前提是您的代码已经在相当小的函数或方法中组织 - 再次因为它往往增强了对代码进行可视化扫描的便利性。

vlcn = VeryLongClassName
lfn = long_function_name
x = vlcn(lfn(arg1, arg2), arg3)
y = vlcn(lfn(arg4, arg5), arg6)

#1


2  

I use the following approach, which scales nicely across a wide variety of situations and tends to keeps lines short -- and thus makes the code easier to scan visually.

我使用以下方法,可以在各种情况下很好地扩展,并且可以保持线条短 - 从而使代码更容易在视觉上扫描。

my_object = VeryLongClassName(
    long_function_name(arg1, arg2),
    arg3,
)

There are a few added benefit of that approach:

这种方法有一些额外的好处:

  • It is widely used when defining large data structures (lists, dicts, and even JSON). It's handy to use a coding style that mimics your data layout style. Code is just another form of data, right?

    它在定义大型数据结构(列表,dicts甚至JSON)时被广泛使用。使用模仿数据布局样式的编码样式非常方便。代码只是另一种形式的数据,对吧?

  • It works great with most text editors, which approach the world from a line-oriented perspective. If each argument to a function or constructor is on a separate line, code refactoring is easy.

    它适用于大多数文本编辑器,它们从面向行的角度来看世界。如果函数或构造函数的每个参数都在一个单独的行上,代码重构很容易。

  • Its application is rule-based and purely mechanical. I never have to puzzle over how to indent the code.

    它的应用是基于规则和纯机械的。我永远不会想知道如何缩进代码。

  • As a result, it looks tidy and the governing principles are immediately clear. As a point of contrast, the indenting examples seen in PEP 8 look like a hodgepodge to my eye and thus don't provide very clear guidance.

    结果,它看起来很整洁,管理原则立即明确。作为对比点,在PEP 8中看到的缩进示例看起来像是我眼中的大杂烩,因此无法提供非常明确的指导。

Another strategy is to use local convenience variables, especially in situations where you need to use long names several times within a method. Although creating short mnemonics runs a risk of making code more obscure, it often helps with readability, provided that your code is already organized in fairly small functions or methods -- again because it tends to enhance the ease of visually scanning of the code.

另一种策略是使用局部便利变量,尤其是在需要在方法中多次使用长名称的情况下。尽管创建短助记符会使代码更加模糊,但它通常有助于提高可读性,前提是您的代码已经在相当小的函数或方法中组织 - 再次因为它往往增强了对代码进行可视化扫描的便利性。

vlcn = VeryLongClassName
lfn = long_function_name
x = vlcn(lfn(arg1, arg2), arg3)
y = vlcn(lfn(arg4, arg5), arg6)