Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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.

like image 462
Adam G Avatar asked Jul 19 '18 17:07

Adam G


People also ask

How are parameters passed in Python?

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

When a parameter is passed to a function it is called?

Call by value:- The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function.

Where must keyword arguments come when calling a function?

Keyword arguments are passed to functions after any required positional arguments. But the order of one keyword argument compared to another keyword argument does not matter. Note how both sections of code below produce the same output.

What is the correct syntax for defining a parameter?

In the function definition f(x) = x*x the variable x is a parameter; in the function call f(2) the value 2 is the argument of the function. Loosely, a parameter is a type, and an argument is an instance. A parameter is an intrinsic property of the procedure, included in its definition.


2 Answers

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.

like image 109
wim Avatar answered Nov 09 '22 05:11

wim


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)
like image 39
ForceBru Avatar answered Nov 09 '22 05:11

ForceBru