Default and Named Values of Arguments

Default and Named Values of Arguments

We have the function trak():

def trak(name,age,profession):
      print(name,"is",age,"old,and he/she is a", profession)

Let’s try to execute it:

>>> trak(“Jerry”,45,”pilot”)
Jerry is 45 old,and he/she is a pilot
>>> trak(“Jane”,23,”student”)
Jane is 23 old,and he/she is a student
>>>      

We should be careful about providing all arguments the function needs. If we forget something, we can see an error:

>>> trak(“John”)
Traceback (most recent call last):
  File “<pyshell#7>”, line 1, in <module>
    trak(“John”)
TypeError: trak() missing 2 required positional arguments: ‘age’ and ‘profession’
>>>

To avoid such errors, we can provide default values of arguments. Let’s define another function:

 def trak(name,age=48,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

 

Now we can provide only one argument to our function:

>>> trak(“John”)
John is 48 old,and he/she is a driver
>>>

>>> trak(“Ann”)
Ann is 48 old,and he/she is a driver
>>>      

The parameter name is still required to provide; the rest aren’t required.

>>> trak()
Traceback (most recent call last):
  File “<pyshell#12>”, line 1, in <module>
    trak()
TypeError: trak() missing 1 required positional argument: ‘name’
>>>

We can provide arguments to our function either in order written in the header of the function or in any order, using their names.

def trak(name=”Alejandro”,age=48,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

>>> trak()
Alejandro is 48 old,and he/she is a driver
>>>

Let’s change the parameters:

def trak(age=48,name=”Alejandro”,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

>>> trak()
Alejandro is 48 old,and he/she is a driver
>>>

When parameters hold default values, we can provide only some of them:

def trak(age=48,name=”Alejandro”,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

>>> trak(name=”Thomas”,age=67)
Thomas is 67 old,and he/she is a driver
>>>

However, parameters at the beginning of the list and on their places, aren’t forced to have their names.

def trak(age=48,name=”Alejandro”,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

>>> trak(“Thomas”,67)

67 is Thomas old,and he/she is a driver
>>>

Oops! Something has gone wrong. The program assigns the argument to the parameters in the following way:

age=Thomas

name=67

To avoid that, we should write:

>>> trak(name=”Thomas”,age=67)
Thomas is 67 old,and he/she is a driver
>>>

But we can do so:

>>> trak(67,”Thomas”)
Thomas is 67 old,and he/she is a driver
>>>

However, parameters being placed after named parameters, even being on their places, have to have their names:

def trak(age=48,name=”Alejandro”,profession=”driver”):
      print(name,”is”,age,”old,and he/she is a”, profession)

>>> trak(age=50,”Rico”,teacher)
SyntaxError: non-keyword arg after keyword arg
>>>

So we should have been written:

>>> trak(age=50,name=”Rico”,profession=”teacher”)
Rico is 50 old,and he/she is a teacher
>>>

Now everything is OK!

When we call a function with named arguments (keywords), the caller identifies the arguments by the parameter name. This solution has one strong advantage: you can skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s