简体   繁体   English

Python中的参数命名约定

[英]Parameter naming convention in Python

For functions with closely related formal parameters, such as 对于具有密切相关的形式参数的函数,例如

def add_two_numbers(n1, n2):
    return n1 + n2

def multiply_two_numbers(n1, n2):
    return n1 * n2

Is it a good idea to give the same names to the parameters in both functions, as shown above? 如上所示,给两个函数中的参数赋予相同的名称是一个好主意吗?

The alternative is to rename the parameters in one of the functions. 另一种方法是重命名其中一个函数中的参数。 For example: 例如:

def add_two_numbers(num1, num2):
    return num1 + num2

Keeping them the same in both functions looks more consistent since the parameters each one takes are analogous, but is that more confusing? 在两个函数中保持相同看起来更加一致,因为每个函数所采用的参数都是类似的,但更令人困惑的是?

Similarly, which would be better for the example below? 同样,对于下面的例子哪个更好?

def count(steps1, steps2):
    a = 0
    b = 0

    for i in range(steps1):
        a += 1

    for j in range(steps2):
        b += 1

    return a, b

def do_a_count(steps1, steps2):
    print "Counting first and second steps..."
    print count(steps1, steps2)

Otherwise, changing the arguments in the second function gives: 否则,更改第二个函数中的参数会给出:

def do_a_count(s1, s2):
    print "Counting first and second steps..."
    print count(s1, s2)

Again, I'm a little unsure of which way is best. 再说一遍,我不确定哪种方式最好。 Keeping the same parameter names makes the relation between the two functions clearer, while the second means there is no possibility of confusing parameters in the two functions. 保持相同的参数名称使两个函数之间的关系更清晰,而第二个意味着不可能在两个函数中混淆参数。

I have done a bit of searching around (including skimming through PEP-8), but couldn't find a definitive answer. 我做了一些搜索(包括浏览PEP-8),但找不到明确的答案。 (Similar questions on SO I found included: Naming practice for optional argument in python function and In Python, what's the best way to avoid using the same name for a __init__ argument and an instance variable? ) (我发现的类似问题包括: python函数中的可选参数的命名练习Python中,避免对__init__参数和实例变量使用相同名称的最佳方法是什么?

I would keep the names the same unless you have a good reason to use different names ... Remember that even positional arguments can be called by keywords, eg: 我会保持名称相同,除非你有充分的理由使用不同的名称...请记住,甚至位置参数可以通过关键字调用,例如:

>>> def foo(a,b):
...     print a
...     print b
... 
>>> foo(b=2,a=1)

Keeping the "keywords" the same helps in that rare, but legal corner case ... 保持“关键字”相同有助于这个罕见但合法的角落案例......

In general, you should give your function's parameters names that make sense, and not even consider anything to do with other functions when you choose those names. 一般来说,你应该给你的函数的参数名称有意义,甚至在选择这些名称时甚至不考虑与其他函数有关。 The names of two functions' parameters simply don't have anything to do with each other, even if the functions do similar things. 两个函数的参数名称之间没有任何关系,即使函数执行类似的操作也是如此。

Although... if the functions do do similar things, maybe that's a sign that you've missed a refactoring? 虽然......如果功能确实做了类似的事情,也许这表明你错过了重构? :) :)

The important guideline here is that you should give them reasonable names—ideally the first names that you'd guess the parameters to have when you come back to your code a year later. 这里重要的指导原则是你应该给他们一些合理的名字 - 理想情况下,当你一年后回到你的代码时,你会猜到参数的名字。

Every programmer has a set of conventions. 每个程序员都有一套约定。 Some people like to call integer arguments to binary functions n1 and n2 ; 有些人喜欢将整数参数调用到二进制函数n1n2 ; others like n , m ; 其他像nm ; or lhs , rhs ; 或者lhsrhs ; or something else. 或者是其他东西。 As long as you pick one and stick to it (as long as it's reasonable), anyone else can read your code, and understand it, after a few seconds learning your style. 只要你选择一个并坚持下去(只要它是合理的),其他任何人都可以在几秒钟后学习你的风格,阅读你的代码并理解它。 If you use different names all over the place, they'll have to do a lot more guessing. 如果你在各处使用不同的名字,他们将不得不做更多的猜测。

As mgilson points out, this allows you to use keyword parameters if you want. 正如mgilson指出的那样,如果你愿意,你可以使用关键字参数。 It also means an IDE with auto-complete is more useful—when you see the (…n1…, …n2…) pop up you know you want to pass two integers. 它还意味着具有自动完成功能的IDE更有用 - 当您看到(…n1…, …n2…)弹出时,您知道要传递两个整数。 But mainly it's for readability. 但主要是为了可读性。

Of course if there are different meanings, give them different names. 当然,如果有不同的含义,给他们不同的名称。 In some contexts it might be reasonable to have add_two_numbers(augend, addend) and multiply_two_numbers(factor, multiplicand) . 在某些情况下,使用add_two_numbers(augend, addend)multiply_two_numbers(factor, multiplicand)可能是合理的。

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

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