使用大量参数编写函数/方法的 Pythonic 方式

发布于 2024-09-07 00:36:21 字数 173 浏览 9 评论 0 原文

想象一下:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa):
    pass

该行超过了 79 个字符,那么,Python 的多行方式是什么?

Imagine this:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa):
    pass

The line overpass the 79 characters, so, what's the pythonic way to multiline it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

醉生梦死 2024-09-14 00:36:21

您可以在括号(或方括号)内包含换行符,例如

def method(self, alpha, beta, gamma, delta, epsilon, zeta,
                 eta, theta, iota, kappa):
    pass

(当然,要包含的空格数量取决于您)

但在这种情况下,您还可以考虑

def method(self, *args):
    pass

和/或

def method(self, **kwargs):
    pass

取决于您如何使用参数(和您希望如何调用该函数)。

You can include line breaks within parentheses (or brackets), e.g.

def method(self, alpha, beta, gamma, delta, epsilon, zeta,
                 eta, theta, iota, kappa):
    pass

(the amount of whitespace to include is, of course, up to you)

But in this case, you could also consider

def method(self, *args):
    pass

and/or

def method(self, **kwargs):
    pass

depending on how you use the arguments (and how you want the function to be called).

风渺 2024-09-14 00:36:21

我认为回答这个问题的“Pythonic”方式是比语法更深入。向方法传递如此多的参数表明您的对象模型可能存在问题。

  1. 首先,您真的需要向此方法传递那么多参数吗?也许这表明这项工作可以在其他地方更好地完成(通过已经有权访问变量的对象)?

  2. 如果这确实是该方法的最佳位置,那么其中一些参数是否可以作为该对象本身的实例变量提供(通过 self)?

  3. 如果没有,你能重新定义父对象的职责来包含它们吗?

  4. 如果没有,您能否将任何单个参数封装在一个复合对象中,以形式化它们之间的关系?如果任何参数有任何共同点,那么这应该是可能的。

I think the 'Pythonic' way of answering this is to look deeper than syntax. Passing in that many arguments to a method indicates a likely problem with your object model.

  1. First of all, do you really need to pass that many arguments to this method? Perhaps this is an indication that the work could be better done elsewhere (by an object that already has access to the variables)?

  2. If this really is the best place for the method, then could some of those arguments be supplied as instance variables of this object itself (via self)?

  3. If not, can you redefine the responsibilities of the parent object to include them?

  4. If not, can you encapsulate any of the individual arguments in a composite object that formalizes the relationship between them? If any of the arguments have anything in common, then this should be possible.

玉环 2024-09-14 00:36:21

我将后续行缩进 2 级:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta,
        theta, iota, kappa):
    pass

I indent the subsequent lines 2 levels:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta,
        theta, iota, kappa):
    pass
红焚 2024-09-14 00:36:21

我会这样写,

def method(
    self, alpha, beta, gamma, delta, epsilon,
    zeta, eta, theta, iota, kappa
):
    pass

我认为它看起来漂亮又干净

I would write it like that

def method(
    self, alpha, beta, gamma, delta, epsilon,
    zeta, eta, theta, iota, kappa
):
    pass

I think it looks nice and clean

━╋う一瞬間旳綻放 2024-09-14 00:36:21

另一种选择是在字典中传递参数。

Another option would be to pass the parameters in a dict.

挽梦忆笙歌 2024-09-14 00:36:21

一旦击中 79,我就分成左括号,如下所示:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota,
           kappa):

当名称太长而无法放在左括号后面时,我会这样做:

x.long_function_is_long(
    long_argument_is_loooooooooooooooooooooooooooooooooooooooong,
    longer_argument_is_looooooooooooooooooooooooooooooooooooooooonger
)

I just split to the open bracket once 79 is hit, like this:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota,
           kappa):

And when names are too long to be put after the opening bracket, I do it like this:

x.long_function_is_long(
    long_argument_is_loooooooooooooooooooooooooooooooooooooooong,
    longer_argument_is_looooooooooooooooooooooooooooooooooooooooonger
)
一百个冬季 2024-09-14 00:36:21

如果您有一个接受如此多变量作为参数的函数,那么您最不需要担心的就是缩进

If you have a function that takes so many variables as arguments, the last thing you have to worry about is indentation

伪装你 2024-09-14 00:36:21

为了便于阅读,当我有很长的名字时,我这样做:

    file = load_sftp_file( SERVER_SFTP_HOST,
                           SERVER_SFTP_USER,
                           SERVER_SFTP_PASSWORD,
                           SERVER_SFTP_PRIVATE_KEY,
                           SERVER_SFTP_PATH,
                           FTP_DIR,
                           specific_filename=self.get_filename())

For readability when I have long names I do this:

    file = load_sftp_file( SERVER_SFTP_HOST,
                           SERVER_SFTP_USER,
                           SERVER_SFTP_PASSWORD,
                           SERVER_SFTP_PRIVATE_KEY,
                           SERVER_SFTP_PATH,
                           FTP_DIR,
                           specific_filename=self.get_filename())
物价感观 2024-09-14 00:36:21

我通常这样做,我不知道这是否是最好的,但它确实有效。因为我必须分割线,所以我让它们同样长(如果可以的话):

def method(self, alpha, beta, gamma, delta, epsilon, \
                 zeta, eta, theta, iota, kappa):
    pass

另外,拥有如此数量的参数,我会建议与 David 相同,使用 *args

I usually do this, I don't know if it's the best, but it works. Since I have to split the line I make them equally longer (if I can):

def method(self, alpha, beta, gamma, delta, epsilon, \
                 zeta, eta, theta, iota, kappa):
    pass

Also, having such quantity of parameters I would recommend the same as David, use *args

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文