简体   繁体   中英

is it wrong to use same names for variables inside the function definition as thevariables used when calling a function in python

basically what the title says.

say i got a function definition

def add_one (num1):
    numOut = num1 +1
    return numOut

and im calling the function as follows

num1 = 11
numOut = add_one (num1)

is this considered bad practice, as we are naming two different objects with the same name, or is it good practice s it increases readability? Or is it something that I am missing completely?

(also checked pass variable to function using same name and Is it normal to use in the function some variable with the same name as it function? but they do not refer to python)

First, lets talk about scopes:

In python and a lot of other languages variables exist within its scope. This means that if you create a variable inside a scope it will stop existing when you leave that scope.

Variables on a outer scope are also accesible within an inner scope but if you asing something to that variable then it will be redeclared as a local variable inside this scope. Meaning that that variable will be diferent than the one on the outer scope.

If you ask me, I don't think is a bad practice to use the same name as the arguments of your functions. In fact I would call bad practice trying to avoid that as you would end up tryining to figure out new names for different variables that have the same meaning.

What you should avoid is to use variables that are not on the local scope (no matter what language are you using).

Try not to worry about that. Sometimes, it can make sense for argument variables to have the same name as the function's parameters, but you never want to enforce that. Your primary concern is that the code that defines a function should make sense regardless of how it might be called.

Remember that you might want to call a function passing some expression other than a plain variable, or that you might want to call the same function more than once in the same scope with different arguments. Trying to match the parameter names of a function you're calling only makes sense if you can't think of some better name for your variables.

When doing this there are always things to consider:

  • Am I overwriting the name of a variable I might need?
  • Am I overwriting the name of the function?
  • In the function's context, did the variable already exist?

However, in python the last point does not matter as outside variables (excluding globals) cannot be accessed within the function's scope.

In the following example, the name of the function is overwritten (which is bad):

def myFunc (a, b):
    myFunc = 2
    if a > 2 and a > b:
        return a
    return b

In this next example the context / scope of the function changes what variables you have access to in languages that are not python:

x = 3

def myFunc (a, b):
    if a + x > b + x:    # in some languages this would work, but in python this is an error because x is not defined in the function.
        x = 5            # here x == 5, because we have redefined it. x is still 3 outside of our function though.
        return a + x
    return b

In the second example, using a variable's value before setting the value is regarded as bad practice, but you don't need to worry about that in python because it's not possible to do.

something that I am missing completely

Important feature of functions in python is their reusability , imagine changing requirementz force you to use your function

def add_one (num1):
    numOut = num1 +1
    return numOut

not for one, but for 2 different variables, you might end with

num1 = 11
num2 = 15
numOut = add_one (num1)
numOut2 = add_one (num2)

Now you have some variables which names are same like in function and same which are different. If you use different names from start not such problem occurs, for example:

x1 = 11
x2 = 15
x1out = add_one(x1)
x2out = add_one(x2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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