简体   繁体   English

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

[英]What's the proper way to break nested function/constructor calls in Python?

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. 超过79个字符。 Should I break like this: 我应该像这样打破:

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

or this? 或这个?

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

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). 它在定义大型数据结构 (列表,dicts甚至JSON)时被广泛使用。 It's handy to use a coding style that mimics your data layout style. 使用模仿数据布局样式的编码样式非常方便。 Code is just another form of data, right? 代码只是另一种形式的数据,对吧?

  • 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法打破 python 中的一系列 function 调用 - Is there way to break out of a series of function calls in python 使Python 3函数接受预设的正确方法是什么? - What's the proper way to make a Python 3 function accept presets? 从函数调用常量时,(PEP8)换行的正确方法是什么? - What's the proper way to (PEP8)line break for when constants are called from a function? 在python中,实现其构造函数可能会失败的类的正确方法是什么? - In python, what's the proper way to implement a class whose constructor could fail? 定义或记录由__getattr__处理的调用的正确方法是什么? - What's the proper way of defining or documenting calls handled by __getattr__? 在python中编写syslog函数的正确方法是什么? - What is the proper way to write a syslog function in python? 在Python中打印具有最高值的嵌套列表的正确方法是什么 - What is the proper way to print a nested list with the highest value in Python 在 Python 中从嵌套字典中提取值的正确方法是什么? - What is the proper way to extract the value from nested dictionary in Python? 在正则表达式 python 中排除大写单词的正确方法是什么 - What's the proper way to exclude uppercase word/s in regex python 在Python中编写游戏循环的正确方法是什么? - What's the proper way to write a game loop in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM