简体   繁体   English

在 python 中缩进 function 调用的正确方法

[英]Correct way to indent function calls in python

Suppose I am declaring some variables like this (I think it's too much indentation):假设我要声明一些这样的变量(我认为缩进太多了):

httpd = Server(
    (
        "",
        conf.port
    ),
    Handler
)

I could do it like this, but it is not very readable:我可以这样做,但它不是很可读:

httpd = Server(("", conf.port), Handler)

or like this (the way in between):或像这样(两者之间的方式):

httpd = Server(
    ("", conf.port),
    Handler
)

And some people are even doing it like this (which is the worst I think):有些人甚至这样做(这是我认为最糟糕的):

httpd = Server(
    ("",
        conf.port),
    Handler)

Question问题

Is there any rule / guide which of these variants should be used with which number of arguments?是否有任何规则/指南应将这些变体中的哪些与 arguments 的数量一起使用? There is a difference between之间有区别

var = function(with="with", many="many",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
              argu="argu", ments="ments",
)

and

var = function(argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", ments="ments",
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
              argu="argu", ments="ments", argu="argu", ments="ments", argu="argu", 
)

As for a guide to these, there is one here: https://www.python.org/dev/peps/pep-0008/#indentation至于这些的指南,这里有一个: https://www.python.org/dev/peps/pep-0008/#indentation

For your specific case I would recommend using:对于您的具体情况,我建议使用:

httpd = Server(
    ("", conf.port),
    Handler
)

and group ("", conf.port) on its own line to differentiate it from Handler.和 group ("", conf.port) 在自己的行上以将其与 Handler 区分开来。

Well, the indentation is one of the unique programming language feature of Python.嗯,缩进是 Python 的独特编程语言功能之一。 But I wouldn't say that there is any correct or right way to call a function with multiple arguments.但我不会说有任何正确或正确的方法来调用具有多个 arguments 的 function。 Most of the time I use function calls with arguments, If the arguments are more than 3, I would use this format:大多数时候我使用 function 和 arguments 调用,如果 arguments 超过 3,我会使用这种格式:

someValue = myFunction(
    first_arg=0, 
    second_arg=5,
    third_arg=10,
    fourth_arg=15,
    fifth_arg=20
)

Or I would just use the normal one:或者我会使用普通的:

someValue = myFunction(first_arg=0)

But it is totally up to you.但这完全取决于你。 Any kind of format is okay and there is no 'Pythonic' way of doing this.任何一种格式都可以,并且没有“Pythonic”的方式来做到这一点。

According to PEP8, the line length should be 79 characters.根据 PEP8,行长应为 79 个字符。 Then, you should wrap any line longer than that.然后,你应该换行比这更长的行。

Though that's only a style guide, not rule or requirement.虽然这只是一个风格指南,而不是规则或要求。 So it's really up to you.所以这真的取决于你。 If you use an IDE, eg pycharm/atom/vscode.如果您使用 IDE,例如 pycharm/atom/vscode。 You can set the max length of the lines and use autoformatting to auto wrap the lines.您可以设置行的最大长度并使用自动格式化来自动换行。 IMO, It's too tedious to to manually. IMO,手动操作太乏味了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM