2

I need help with parameteres. Do both of these function definitions do the exact same thing for print_twice?

def print_twice(lol):
    print lol
    print lol

def print_twice(michael):
    print michael
    print michael

If yes, then I'm guessing the word used for the parameter doesn't matter, correct?

Seb
  • 1,966
  • 2
  • 17
  • 32
  • 1
    Contrary to the answers, there *are* differences callers might observe, but only one could ever matter in reasonable code, even then it's rare and obscure, and it won't help you learning. Thus I won't make an answer out of this. For the next months, just go by the answers that say "yes, they are identical". –  Mar 17 '12 at 21:17

6 Answers6

5

The word we use for the parameter does matter. It is important that the word you use:

  • is meaningful and clearly explains what the argument is for,
  • does not override some variable name from the external scope.

Importance of meaningful arguments' names

The name you use for argument is important, because the names of the arguments, their default values and the function name are the things developers using your function first see, even without the need to look into function documentation (eg. by using help(your_function)). Just use IDLE to define your function and then try to use it - when writing it, the IDLE will show you possible arguments.

So please, give them meaningful names that will make using your function easier and will not require looking into the documentation.

Overriding variables from outer scopes

When it comes to the second point, just look at this example:

def show_elements(elements):
    """Shows elements of the passed argument
    """
    for element in elements:
        print element

which works ok, but if you replace elements with eg. list, you will override list within this specific scope:

def show_elements(list):
    """Shows elements of the passed argument
    """
    for element in list:
        print element

and then if you would like to use list eg. for building a list, or converting from other type into list, then you will have problems. list is a builtin and you should not override it. Similar is true also about the other variables from the scopes surrounding the function.

Historically, when Python was resolving variable names by first looking into local scope, then global and builtin scopes, skipping all nonlocal ones (eg. scope from the function in which our function was defined), enclosing scope's variables were passed that way:

def funca():
    local_val1 = 'some value1'
    local_val2 = 'some value2'
    def funcb(local_val1=local_val1):
        # local_val1 is accessible here, even though local_val2 is not
        ...
    ...

But since the above is no longer true, you will need to take surrounding scopes into account, thus using non-conflicting name is important.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
2

Yes they do. The name of a parameter is irrelevant, although good programming practices mandate that the name should be clear and self-explanatory

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

That's correct, the name of the parameter doesn't matter.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

Programming is supposed to be logical. So, let's think logically. If you write "a" on a paper twice, you get "a" two times. Same with "b." So you're doing the same function with letters. But what if you reassigned a value to a, each time a went through the function. I mean, what if a was a number, then IMO the closest you could get is something like this:

def func1(a, b):
    a = input("Enter a number: ")
    b = input("Enter another number: ")
    b *= b
    a *= a
    print func1(a)
    print func1(a)
    print func1(b)
    print func1(b)

Now, when I try to compile this specific code online, I get an error but I think something like this will work for the sake of trying to do what you're doing if done correctly? It's a good experiment, and I would imagine some usage in it.

user2932
  • 137
  • 3
  • I know it's probably like you look at my code and you think "No kidding!" I meant that as in what I said might have been obvious to some. – user2932 Jun 02 '14 at 04:57
0

yes that is correct its just a variable name ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

That is correct. The word used for the parameter in the function definition is only a name, and does not refer to anything external.

Recaiden
  • 58
  • 1
  • 4