Step 2 - Keyword Arguments

In Step 1 we used positional arguments.

  • Create a new file called lab_4_step_2_keyword_arguments.py and add the following code:
import boto3

def translate_text(text, source_language_code, target_language_code): 
    client = boto3.client('translate')
    response = client.translate_text(
        Text=text, 
        SourceLanguageCode=source_language_code, 
        TargetLanguageCode=target_language_code
    )
    print(response) 

def main():
    translate_text('I am learning to code in AWS','en','fr')

if __name__=="__main__":
    main()

In the next step, we are going to replace the positional arguments with keyword arguments.

A keyword argument is a name-value pair that is passed to the function. Here are some of the advantages:

  • If the values passed with positional arguments are wrong, you will get an error or unexpected behavior. Keyword arguments can be passed in any order.
  • When used with the ** we can pass an arbitrary number of keyword arguments.
  • We can reduce the number of lines in our code.

Modifying the function to use keyword arguments

Modify your function as follows:

import boto3

def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response) 

def main():
    translate_text(Text='I am learning to code in AWS',SourceLanguageCode='en',TargetLanguageCode='fr')

if __name__=="__main__":
    main()
  • To run the program, enter the following command in the terminal:

    python lab_4_step_2_keyword_arguments.py.

What did we do?

  • We replaced the positional arguments with keyword arguments. This is done using the **kwargs.
  • We removed all the parameters such as Text=text which shortens our code and replaced it with response = client.translate_text(**kwargs).
  • We defined the keyword arguments when we called the function using the syntax Text='I am learning to code in AWS'.

What did python do?

  • Python used the keyword arguments defined when we called the function and passed them to the function as variables.

Try this

  • Swap the order of the keyword arguments around. Did it generate an error?

Using dictionaries as keyword arguments

In the last example, we defined keyword arguments using the syntax key=value. In the dictionaries part of the workshop we learned that dictionaries use a very similar format of "key":"value"

AWS will often return information in the dictionary "key":"value" format. We can use these as our keyword values in our function easily.

Next, modify how we call the function. Change the syntax from key=value to "key":"value"

import boto3

def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response) 

### Change below this line only ###

kwargs={
    "Text":"I am learning to code in AWS",
    "SourceLanguageCode":"en",
    "TargetLanguageCode":"fr"
    }

def main():
    translate_text(**kwargs)

if __name__=="__main__":
    main()

What did we do?

  • We defined a variable called kwargs containing a dictionary of key:value pairs.
  • We replaced the keyword arguments when we called the function with **kwargs. The ** tells python that it is an arbitrary number of arguments, kwargs is the function name we defined.
  • We put each key:value pair on a separate line to make it easy to read.

kwargs does not have to be used. You can define the name used.

What did python do?

  • Python used the key:value pairs defined in the dictionary in place of the keyword arguments

Being able to pass a dictionary of key:value pairs rather than keyword pairs is useful because in AWS inputs to programs often use dictionaries.