12

I've been learning python recently and I'm a little confused to why people name their parameters when calling the function their naming the parameters to?

Take this code for starter

def my_funcation(greeting = 'Hello', name = 'Guest'): return f'{greeting}, {name}. How are you?' print(my_function('Yo', name = 'Adam')) 

It all looks good, but there's one part I don't get. Why do people specifiy the parameter name their assigning to? Is this like a convention or is it a rule to write good code?

Why can't we just write this..

def my_funcation(greeting = 'Hello', name = 'Guest'): return f'{greeting}, {name}. How are you?' print(my_function('Yo', 'Adam')) 

IMO, the second one is better, incase the parameter name ever changes.

4
  • 3
    If you have multiple optional variables, and you only want to set one of them, you have to use this syntax. In your examples it doesn't matter, but if you only wanted to change name for example, you would have to explicitly use my_function(name='Chris'), while greeting would default to 'Hello' Commented Jul 19, 2018 at 17:46
  • The ones in the function definition are default values. If you don't provide a value for that when calling the function, it will still work. If you provide a value when calling it, it will override the one that's the default. Commented Jul 19, 2018 at 17:47
  • Your question is about why people name their parameters in case they have default values or in case some parameters have default values and some no or in case there are no parameters with default values? Commented Jul 19, 2018 at 17:53
  • "If the parameter name ever changes" -> this would be an argument AGAINST using ordered arguments and FOR using argument names, because if the name of the parameter(argument) changes, then the function that it performed probably also changed (if it didn't, then it didn't need to be renamed... oh, for readability, no, sorry, it's been "published" with that name, if you need to improve readability later, assign it internally to a better named variable). Commented Apr 8, 2019 at 8:19

3 Answers 3

19

Sometimes, it adds helpful context to the code.

What would you rather read:

enumerate('xyz', 120) # err...what does that second arg do again? 

or

enumerate('xyz', start=120) # oh yeah, the start index 

It allows you to modify or reorder the kwargs of the function, with no change required for the calling code.

Consider adding a new argument, in front:

def my_function(color='red', greeting='Hello', name='Guest'): ... 

If you don't also modify the caller my_function('Yo', 'Adam'), you would have them filling the incorrect arguments now - and if you don't have a good test coverage, you might not even notice the bug until too late.

Sign up to request clarification or add additional context in comments.

Comments

2

What if you don't want to specify all the parameters?

def my_function(greeting = 'Hello', name = 'Guest'): return f'{greeting}, {name}. How are you?' print(my_function(name='ForceBru')) 

You won't be able to specify just the second argument without specifying the one before it without this feature.


Or, what if your function has tons of arguments, but:

  1. many of them have default values
  2. you don't want to clutter the function declaration
  3. you want the function call to reflect what are the parameters you're passing

What do you do then? Basically the same thing:

def compute(stuff, **kwargs): be_fast = kwargs.get('be_fast') be_super_fast = kwargs.get('be_super_fast') if be_fast and be_super_fast: print('Activating turbo mode...') elif be_fast: print('Accelerating...') elif be_super_fast: print('Nobody can stop me!') else: print('I am a tortoise.') return stuff 

And then you can call this as follows:

compute(1) compute(1, be_fast=True) compute(1, be_super_fast=True) compute(1, be_fast=True, be_super_fast=True) 

Comments

-1

What if you want to say "Hello, Adam. How are you?",

You need to use print(my_function(name = 'Adam'))

since using,

print(my_function('Hello', 'Adam')) 

would be redundant.

IMO, the second one is better, in case the parameter name ever changes.

In this specific case it is. But in general I wouldn't say better, but rather different.

Comments