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.
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.
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.
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.
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.
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
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.
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:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With