What is the point of using a keyword?

Hello all, first timer here.

I was wondering what the point of keywording something in the parameter is ? In the example of the exercise that have been mentioned in this thread:

def greet_customer(special_item, grocery_store=“Engrossing Grocer’s”):
print("Welcome to "+ grocery_store + “.”)
print("Our special is " + special_item + “.”)
print(“Have fun shopping!”)

Engrossing Grocer’s has now been keyworded into the parameter’s positional arguements.

It seems to me that this is extra work and redundant coding, since keywording something to a parameter is pretty much the same as me doing this :

def greet_customer(special_item):
print(“Welcome to Engrossing Grocer’s.”)
print("Our special is " + special_item + “.”)
print(“Have fun shopping!”)

now isn’t that pretty much the same as me keywording it into the parameter ? but with less code ?
or does keywording something as in the example have broader uses ?

also, on a side note. How do I store a functions output as a variable ?

ex:

def somernumbers(number, x, y)
print(number + x*y)

somenumbers(5, 3, 2)

now that prints the number to the console, but
how do I go about storing that number/function as a variable ? I’ve tried all sorts of things, but with no luck.

Thank you all so much in advance.

It is not the same. That example has hard coded a literal into the function body, and does not allow for a second positional argument. By keywording a default value, we may still replace that with a literal argument in subsequent calls.

>>> def add_to_list(temp_list=[], item='temp_item'):
    temp_list.append(item)
    return temp_list

>>> my_list = add_to_list()
>>> my_list
['temp_item']
>>> my_list = add_to_list(['existing item'])
>>> my_list
['existing item', 'temp_item']
>>> my_list = add_to_list(['existing item'], 'new item')
>>> my_list
['existing item', 'new item']
>>> 
31 Likes

Actually when you are assigning the parameter value “Engrossing Grocer’s” is the default parameter in this case, it means that if no argument are passed while calling greet_customer() is specified then the default value of argument is taken by default.

1 Like

The reason it’s helpful to use defaults is because it’s actually less code most of the time, if the default value is the same, but we can still adjust it as needed.

For example we might have a round() function that takes in two values 1 - a number to be “rounded”, and 2 - the number of decimals points we want to round it to.

Now we might know that we usually need it to be rounded to 2 decimals places (1.00) so we can set that as the default like so: round(number, decimal places = 2), and now whenever we call round() if we just want it to be rounded to 2 places like we know we usually will we can just call for example round(14.564) and this will round it to 14.56

In short, keywording allows us to have defaults values, which can usually save time and produce much simpler more readable code, when only one of multiple parameters are usually being put in differently each time.

11 Likes

Very good example of why to use keywording. Thank you.

So I get why including a default value can make a lot of sense. That’s really cool. But I have a question about the other portion of this lesson. It says that this will work:

def greet_customer(grocery_store, special_item):

print(“Welcome to” + grocery_store + “.”)
print("Our special is " + special_item + “.”_
print(“Have fun shopping!”)

greet_customer(special_item = “chips and salsa”, grocery_store = “Stu’s Staples”)

Tested it and it works fine.

Is there a scenario where I would want to do this? It seems like it’d just be easier and more efficient to call:

greet_customer("Stu’s Staples, “chips and salsa”)

So what am I missing? Why would I want to swap the positional arguments. Because I assume the ability to do this was included for a reason. I just can’t figure out why.

1 Like

It makes more sense for a callable with a few parameters. Consider the built-in print-Built-in Functions — Python 3.9.2 documentation

If you focus on the default arguments you’ll see there are four of them, sep, end, file and flush-
print ( *objects , sep=’ ’ , end=’\n’ , file=sys.stdout , flush=False )

Let’s focus on end (you can ignore the rest for now). All end does by default is put a newline character at the end of whatever you printed. If you wanted to change this you can pass a keyword argument to end, for example-

print(3, end="")  # remove the new line
print("red", end="\n----\n")  # a new line, a mark and another new line

This saves you having to pass multiple arguments just to alter a single parameter (in this example all we skipped was sep but consider how much effort it would be to alter flush without a keyword).

This is less effort to type and much more readable than passing every single argument and you don’t even need to know the default values for the other parameters, just pass a keyword for the one(s) you do want to change.