This notebook is largely based on material of the Python Scientific Lecture Notes (https://scipy-lectures.github.io/), adapted with some exercises.

Reusing code

Rule of thumb:

  • Sets of instructions that are called several times should be written inside **functions** for better code reusability.
  • Functions (or other bits of code) that are called from several scripts should be written inside a **module**, so that only the module is imported in the different scripts (do not copy-and-paste your functions in the different scripts!).

Functions

Function definition

Function blocks must be indented as other control-flow blocks


In [1]:
def the_answer_to_the_universe():
    print(42)

the_answer_to_the_universe()


42

Note: the syntax to define a function:

  • the def keyword;
  • is followed by the function’s name, then
  • the arguments of the function are given between parentheses followed by a colon.
  • the function body;
  • and return object for optionally returning values.

Return statement

Functions can optionally return values


In [2]:
def area_square(edge):
    return edge ** 2

area_square(2.3)


Out[2]:
5.289999999999999

Parameters

Mandatory parameters (positional arguments)


In [3]:
def double_it(x):
    return 2*x

double_it(3)


Out[3]:
6

In [4]:
double_it()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-51cdedbb81b0> in <module>()
----> 1 double_it()

TypeError: double_it() missing 1 required positional argument: 'x'

Optional parameters (keyword or named arguments)

The order of the keyword arguments does not matter, but it is good practice to use the same ordering as the function's definition

Keyword arguments are a very convenient feature for defining functions with a variable number of arguments, especially when default values are to be used in most calls to the function.


In [5]:
def double_it (x=1):
    return 2*x

print(double_it(3))


6

In [6]:
print(double_it())


2

In [7]:
def addition(int1=1, int2=1, int3=1):
    return int1 + 2*int2 + 3*int3

print(addition(int1=1, int2=1, int3=1))


6

In [8]:
print(addition(int1=1, int3=1, int2=1)) # sequence of these named arguments do not matter


6
NOTE:

Default values are evaluated when the function is defined, not when it is called. This can be problematic when using mutable types (e.g. dictionary or list) and modifying them in the function body, since the modifications will be persistent across invocations of the function. Using an immutable type in a keyword argument:

In [9]:
bigx = 10

def double_it(x=bigx):
    return x * 2


bigx = 1e9
double_it()


Out[9]:
20

Using an mutable type in a keyword argument (and modifying it inside the function body)


In [10]:
def add_to_dict(args={'a': 1, 'b': 2}):
    for i in args.keys():
        args[i] += 1
    print(args)

In [11]:
add_to_dict
add_to_dict()
add_to_dict()
add_to_dict()


{'b': 3, 'a': 2}
{'b': 4, 'a': 3}
{'b': 5, 'a': 4}

In [12]:
#the {'a': 1, 'b': 2} was created in the memory on the moment that the definition was evaluated

Alternative to overcome this problem:


In [13]:
def add_to_dict(args=None):
    if not args:
        args = {'a': 1, 'b': 2}
        
    for i in args.keys():
        args[i] += 1
        
    print(args)

In [14]:
add_to_dict
add_to_dict()
add_to_dict()


{'b': 3, 'a': 2}
{'b': 3, 'a': 2}

Variable number of parameters

Special forms of parameters:

  • *args: any number of positional arguments packed into a tuple
  • **kwargs: any number of keyword arguments packed into a dictionary

In [15]:
def variable_args(*args, **kwargs):
    print('args is', args)
    print('kwargs is', kwargs)

variable_args('one', 'two', x=1, y=2, z=3)


args is ('one', 'two')
kwargs is {'y': 2, 'x': 1, 'z': 3}

Docstrings

Documentation about what the function does and its parameters. General convention:


In [ ]:
def funcname(params):
    """Concise one-line sentence describing the function.
    
    Extended summary which can contain multiple paragraphs.
    """
    # function body
    pass

funcname?

Functions are objects

Functions are first-class objects, which means they can be:

  • assigned to a variable
  • an item in a list (or any collection)
  • passed as an argument to another function.

In [ ]:
va = variable_args
va('three', x=1, y=2)

Methods

Methods are functions attached to objects. You’ve seen these in our examples on lists, dictionaries, strings, etc...

Calling them can be done by dir(object):