Steven Maude
Steven Maude

Reputation: 886

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. (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?)

Upvotes: 4

Views: 2723

Answers (3)

abarnert
abarnert

Reputation: 365697

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; others like n, m; or lhs, rhs; 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. 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. 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).

Upvotes: 3

Karl Knechtel
Karl Knechtel

Reputation: 61508

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? :)

Upvotes: 3

mgilson
mgilson

Reputation: 309891

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, e.g.:

>>> 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 ...

Upvotes: 9

Related Questions