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?
The word we use for the parameter does matter. It is important that the word you use:
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:
which works ok, but if you replace
elementswith eg.list, you will overridelistwithin this specific scope:and then if you would like to use
listeg. for building a list, or converting from other type into list, then you will have problems.listis 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:
But since the above is no longer true, you will need to take surrounding scopes into account, thus using non-conflicting name is important.