Why Default Arguments?

Default arguments add a wonderful level of robustness to applications because they allow for some flexibility that is not offered by the standard positional parameters. That gift comes in the form of simplicity for the applications programmer. Life is not as complicated when there are a fewer number of parameters that one needs to worry about. This is especially helpful when one is new to an API interface and does not have enough knowledge to provide more targeted values as arguments.

The concept of using default arguments is analogous to the process of installing software on your computer. How often does one chose the “default install” over the “custom install?” I would say probably almost always. It is a matter of convenience and know-how, not to mention a timesaver. And if you are one of those gurus who always chooses the custom install, please keep in mind that you are one of the minority.

Another advantage goes to the developer, who is given more control over the software they create for their consumers. When providing default values, they can selectively choose the “best” default value possible, thereby hoping to give the user some freedom of not having to make that choice. Over time, as the user becomes more familiar with the system or API, they may eventually be able to provide their own parameter values, no longer requiring the use of “training wheels.”

Here is one example where a default argument comes in handy and has some usefulness in the growing electronic commerce industry:

>>> def taxMe(cost, rate=0.0825):
…       return cost + (cost * rate)
…
>>> taxMe(100)
108.25
>>>
>>> taxMe(100, 0.05)
105.0

In the example above, the taxMe() function takes the cost of an item and produces a total sale amount with sales tax added. The cost is a required parameter while the tax rate is a default argument (in our example, 8¼ %). Perhaps you are an online retail merchant, with most of your customers coming from the same state or county as your business. Consumers from locations with different tax rates would like to see their purchase totals with their corresponding sales tax rates. To override the default, all you have to do is provide your argument value, such as the case with taxMe(100, 0.05) in the above example. By specifying a rate of 5%, you provided an argument as the rate parameter, thereby overriding or bypassing its default value of 0.0825.

All required parameters must be placed before any default arguments. Why? Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxError is raised if the arguments are not given in the correct order:

>>> def taxMe2(rate=0.0825, cost):
…       return cost * (1.0 + rate)
…
SyntaxError: non-default argument follows default argument

Let us take a look at keyword arguments again, using our old friend net_conn().

						def net_conn(host, port):
    net_conn_suite
					

As you will recall, this is where you can provide your arguments out-of-order (positionally) if you name the arguments. With the above declarations, we can make the following (regular) positional or keyword argument calls:

• net_conn('kappa', 8000)
• net_conn(port=8080, host='chino')

However, if we bring default arguments into the equation, things change, though the above calls are still valid. Let us modify the declaration of net_conn() such that the port parameter has a default value of 80 and add another argument named stype (for server type) with a default value of 'tcp':

						def net_conn(host, port=80, stype='tcp'):
    net_conn_suite
					

We have just expanded the number of ways we can call net_conn(). The following are all valid calls to net_conn():

• net_conn('phaze', 8000, 'udp')       # no def args used
• net_conn('kappa')                    # both def args used
• net_conn('chino', stype='icmp')      # use port def arg
• net_conn(stype='udp', host='solo')   # use port def arg
• net_conn('deli', 8080)               # use stype def arg
• net_conn(port=81, host='chino')      # use stype def arg

What is the one constant we see in all of the above examples? The sole required parameter, host. There is no default value for host, thus it is expected in all calls to net_conn().

Keyword arguments calling prove useful for being able to provide for out-of-order positional arguments, but, coupled with default arguments, they can also be used to “skip over” missing arguments as well, as evidenced from our examples above.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset