user5405648
user5405648

Reputation:

Python: When should we name the parameters we're passing?

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.

Upvotes: 12

Views: 2557

Answers (3)

ForceBru
ForceBru

Reputation: 44838

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)

Upvotes: 2

wim
wim

Reputation: 362587

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.

Upvotes: 19

scharette
scharette

Reputation: 9977

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.

Upvotes: -1

Related Questions