Many examples from the arcpy documentation's examples use positional parameter method, which is simply providing the parameter values in the order they appear in the function declaration and the value will be assigned to that parameter name. The first positional parameter will be assigned the first value given within the parentheses (…) when the function is called, and so on. To skip a parameter, you will often use the two quotes ' "" ' or None so that parameter is set and you can set subsequent parameters to keep the positional assignments matching. Below is a simple function with two required parameters. The first parameter for providing the last name of a person, and the second parameter for providing a form of address to demonstrate:
def greet(lastName, formOfAddress):
return f'Hello {formOfAddress} {lastName}!'
print(greet('Smith', 'Mrs.'))Output: Hello Mrs. Smith!
Note how the first value used in the function call "Smith" in line 6 is assigned to the first positional parameter lastName and the second value "Mrs." to the second positional parameter formOfAddress.
The parameter list of a function definition can also contain one or more optional parameters. These are created by using a parameter name and setting a default value in the function declaration.
<argument name>=<default value>
Calling a function with explicitly setting values to the parameter name is referred to as keyword argument assignment using the same notation:
<name>=<value/expression>
Here is a new version of our greet function that now supports English and Spanish, but with English being the default language:
def greet(lastName, formOfAddress, language='English'):
greetings = {'English': 'Hello', 'Spanish': 'Hola'}
return f'{greetings[language]} {formOfAddress} {lastName}!'
print(greet('Smith', 'Mrs.'))
print(greet('Rodriguez', 'Sr.', language='Spanish'))Output: Hello Mrs. Smith! Hola Sr. Rodriguez!
Compare the two different ways in which the function is called in lines 8 and 10. In line 8, we do not provide a value for the "language" parameter so the default value "English" is used when looking up the proper greeting in the dictionary stored in variable greetings. In the second version in line 10, the value ‘Spanish’ is provided for the keyword argument "language", so this is used instead of the default value and the person is greeted with "Hola" instead of "Hello". Keyword arguments can be used like positional arguments meaning the second call could also have been:
print(greet('Rodriguez', 'Sr.', 'Spanish'))without the “language=” before the value.
Things get more interesting when there are several keyword arguments, so let’s add another one for the time of day:
def greet(lastName, formOfAddress, language = 'English', timeOfDay = 'morning'):
greetings = {'English': {'morning': 'Good morning', 'afternoon': 'Good afternoon'},
'Spanish': {'morning': 'Buenos dias', 'afternoon': 'Buenas tardes'}}
return f'{greetings[language][timeOfDay]}, {formOfAddress} {lastName}!'
print(greet('Smith', 'Mrs.'))
print(greet('Rodriguez', 'Sr.', language='Spanish', timeOfDay='afternoon'))Output: Good morning, Mrs. Smith! Buenas tardes, Sr. Rodriguez!
Since we now have four different forms of greetings depending on two parameters (language and time of day), we now store these in a dictionary in variable greetings that for each key (= language) contains another dictionary for the different times of day. For simplicity reasons, we left it at two times of day, namely “morning” and “afternoon.” In line 7, we then first use the variable language as the key to get the inner dictionary based on the given language and then directly follow up with using variable timeOfDay as the key for the inner dictionary.
The two ways we are calling the function in this example are the two extreme cases of (a) providing none of the keyword arguments, in which case default values will be used for both of them (line 10), and (b) providing values for both of them (line 12). However, we could now also just provide a value for the time of day if we want to greet an English person in the afternoon:
print(greet('Rogers', 'Mrs.', timeOfDay='afternoon'))Output: Good afternoon, Mrs. Rogers!
This is an example in which we have to use the prefix “timeOfDay=” because if we leave it out, it will be treated like a positional parameter and used for the parameter ‘language’ instead which will result in an error when looking up the value in the dictionary of languages. For similar reasons, keyword arguments must always come after the positional arguments in the definition of a function and in the call. However, when calling the function, the order of the keyword arguments doesn’t matter, so we can switch the order of ‘language’ and ‘timeOfDay’ in this example:
print(greet('Rodriguez', 'Sr.', timeOfDay='afternoon', language='Spanish'))It is also possible to have function definitions that only use optional keyword arguments in Python.