<< Chapter < Page Chapter >> Page >

The calling program can ignore none, some, or all of the keyword arguments when calling the function. By this I mean that the calling program can simplynot pass parameters for keyword arguments. Parameters that are passed for keyword arguments can be passed in any order. In other words, unlike defaultarguments, the calling program can ignore a keyword argument in the middle of a group of keyword arguments.

As with default arguments, when the calling program ignores keyword arguments, the defined default values for those arguments are used by the code in the body of the function.

The program shown in Listing 4 illustrates a function with one required argument ( listA ) and three keyword arguments ( listB , listC , and listD ).

Listing 4 . Illustration of keyword arguments.
# Illustrates keyword arguments #---------------------------------------------------def listModifier(listA,listB=["B"],listC=["C"],listD=["D"]):"""Illustrates keyword arguments""" print("In listModifier")listA.append(1.00001) print("listA = " + str(listA))listB.append(2.00002) print("listB = " + str(listB))listC.append(3.00003) print("listC = " + str(listC))listD.append(4.00004) print("listD = " + str(listD))return #End function definitionaList = ["ab","cd","ef"] bList = ["The","old","list"]cList = ["This old house"] dList = ["is falling down"]print("aList = " + str(aList)) print("bList = " + str(bList))print("cList = " + str(cList)) print("dList = " + str(dList))print("Call listModifier") listModifier(aList,listD=dList,listB=bList)print("Back from listModifier") print("aList = " + str(aList))print("bList = " + str(bList)) print("cList = " + str(cList))print("dList = " + str(dList))

The code in Listing 4 produces the output shown in Figure 5 .

Figure 5 . Output produced by the code in Listing 4.
aList = ['ab', 'cd', 'ef'] bList = ['The', 'old', 'list']cList = ['This old house'] dList = ['is falling down']Call listModifier In listModifierlistA = ['ab', 'cd', 'ef', 1.00001] listB = ['The', 'old', 'list', 2.00002]listC = ['C', 3.00003] listD = ['is falling down', 4.00004]Back from listModifier aList = ['ab', 'cd', 'ef', 1.00001]bList = ['The', 'old', 'list', 2.00002] cList = ['This old house']dList = ['is falling down', 4.00004]

As before, the first thing that you should pay attention to is the syntax for defining keyword arguments in the function named listModifier in Listing 4 . If you compare it with the argument list for the function with the same namein Listing 3 , you will see that there are exactly the same . Therefore, the manner in which the arguments are defined does not distinguish default argumentsfrom keyword arguments. The distinguishing factor is the syntax with which the function is called.

The second thing you should pay attention to is the syntax used to call the function named listModifier about six lines up from the bottom of Listing 4 . The function call passes three parameters: aList , dList , and bList .

The reference variable named aList is passed as the first parameter to satisfy the required argument named listA . Note that it is passed using the same syntax as in Listing 1 , Listing 2 , and Listing 3 . Because it is a required argument, it must be passed in the correct order in a positionalsense.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask