An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast...

Post on 12-Oct-2020

1 views 0 download

transcript

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

An Introduction to python

Lecture 03: Functions

Roger Wolf23. Mai 2018

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 1:

The python function

1/20

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

● Functions in python are defined as shown in the example below:

defines a function

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

defines a function

function name

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

defines a function

function name

function arguments

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

max(x,y) can have a return value

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

Here the function is called

max(x,y) can have a return value

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

Here the function is called

● Note: the return value is not type bound! Also the arguments are not strictly tested for their type. As long as the types of both arguments provide a “<” operator in our example the function will give a return value.

max(x,y) can have a return value

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The python function2/20

Here the function is called

● Note: the return value is not type bound! Also the arguments are not strictly tested for their type. As long as the types of both arguments provide a “<” operator in our example the function will give a return value.

● Note: This is in contrast to C++, which is statically typing (see Lecture­01). Something like function overloading, like in C++ is not necessary in python.

max(x,y) can have a return value

● Functions in python are defined as shown in the example below:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Checking types3/20

● Still python safes type information that can be checked:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Checking types3/20

You can check the type of each variable using the function type()

● Still python safes type information that can be checked:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The lambda function4/20

● In python you can also define functions at runtime, which are not bound to a name and can fulfill simple tasks:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The lambda function4/20

● In python you can also define functions at runtime, which are not bound to a name and can fulfill simple tasks:

This is a normal (named) function

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The lambda function4/20

● In python you can also define functions at runtime, which are not bound to a name and can fulfill simple tasks:

This is a normal (named) function

This is a (nameless) lambda function; g is a “pointer” to this function

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The lambda function4/20

● In python you can also define functions at runtime, which are not bound to a name and can fulfill simple tasks:

This is a normal (named) function

This is a (nameless) lambda function; g is a “pointer” to this function

This is the same lambda function w/o any use of a name. The second set of braces passes the argument

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 2:

Global vs. local variables

5/20

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Global vs. local variables6/20

● Per default function arguments are passed “by value”, i.e. a variable that is defined in a global scope can only be changed locally inside a function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Global vs. local variables6/20

glob is copied into x here

● Per default function arguments are passed “by value”, i.e. a variable that is defined in a global scope can only be changed locally inside a function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Global vs. local variables6/20

glob is copied into x here

x changes its value from 10 to 5 here

● Per default function arguments are passed “by value”, i.e. a variable that is defined in a global scope can only be changed locally inside a function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Global vs. local variables6/20

glob is copied into x here

But glob kept its initial value

x changes its value from 10 to 5 here

● Per default function arguments are passed “by value”, i.e. a variable that is defined in a global scope can only be changed locally inside a function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Global vs. local variables6/20

glob is copied into x here

But glob kept its initial value

x changes its value from 10 to 5 here

● Local variables, which are defined in fn(), will be freed and are not accessible any more after fn() is left.

● Per default function arguments are passed “by value”, i.e. a variable that is defined in a global scope can only be changed locally inside a function:

● Global variable are “shadowed” by local scopes, i.e. glob is not visible inside fn().

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Another difference to C++7/20

● Note: different from C++ if you define a variable e.g. in a statement block of an if-statement, this variable will still be accessible after that statement block has been left.

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Another difference to C++7/20

● Note: different from C++ if you define a variable e.g. in a statement block of an if-statement, this variable will still be accessible after that statement block has been left. j is still

accessible after the ifTry the same for i=0.

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions8/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-1: – the naive approach –

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions8/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-1: – the naive approach –

Just reference glob in fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions8/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-1: – the naive approach –

Just reference glob in fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions9/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-2: – pass as function argument –

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions9/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-2: – pass as function argument –

Pass as argument to fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions9/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-2: – pass as function argument –

Pass as argument to fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions10/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-3: – using the global declaration –

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions10/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-3: – using the global declaration –

Declare glob as global variable in fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions10/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-3: – using the global declaration –

Declare glob as global variable in fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions10/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-3: – using the global declaration –

Declare glob as global variable in fn()

● Note: you can declare more than one global variable with one global statement, e.g.: global x,y,z

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

References to global variables in functions10/20

● What if you want a reference to a global variable that you need to modify inside a function:

Trial-4: – and don’t forget the obvious –

Change glob upon return from fn()

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

A word on the global declaration10/20

● If you can copy variables into function arguments and again via return, why would you need the global declaration at all?

● Imagine glob being not a simple number, but a GB huge object that cannot be copied around w/o filling your active memory. In this case you cannot pass glob as argument, but you have to manipulate glob itself. In this situation the global statement becomes vital for you.

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

A word on the global declaration10/20

● If you can copy variables into function arguments and again via return, why would you need the global declaration at all?

● Imagine glob being not a simple number, but a GB huge object that cannot be copied around w/o filling your active memory. In this case you cannot pass glob as argument, but you have to manipulate glob itself. In this situation the global statement becomes vital for you.

● Note: this discussion is related to an extended discussion about “call by value” and “call by reference” in C(++) (see e.g. An Introduction to C++ Lecture­01).

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 3:

Function arguments

11/20

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Default arguments12/20

● You can have default arguments for a function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Default arguments12/20

● You can have default arguments for a function:

Default argument

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Default arguments12/20

● You can have default arguments for a function:

Default argument

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Default arguments12/20

● You can have default arguments for a function:

● Note: like in C++ default arguments have to be given at the end of the argument list (see An Introduction to C++ Lecture­04).

Default argument

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Keyword arguments13/20

● You can refer to the arguments by their names during function call:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Keyword arguments13/20

default arguments

● You can refer to the arguments by their names during function call:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Keyword arguments13/20

● You can refer to the arguments by their names during function call:

default arguments

Calling the function with keyword arguments

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Keyword arguments13/20

keyword arguments

Calling the function with keyword arguments

● You can refer to the arguments by their names during function call:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 4:

Function documentation

14/20

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString15/20

● A string in the first logical line of a function is identified as documentation string (DocString):

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString15/20

● A string in the first logical line of a function is identified as documentation string (DocString):

By convention people use as a DocString a multi-line string starting the first line with a capital letter and ending with a dot. This is followed by an empty line and more detailed explanations following…

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString15/20

● A string in the first logical line of a function is identified as documentation string (DocString):

By convention people use as a DocString a multi-line string starting the first line with a capital letter and ending with a dot. This is followed by an empty line and more detailed explanations following…

● Note: this works equally well for modules (see Lecture­04) and classes (see Lecture­06)

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString16/20

● You can access the DocString via the __doc__ attribute belonging the function:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString16/20

● You can access the DocString via the __doc__ attribute belonging the function:

Access the DocString

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString16/20

● You can access the DocString via the __doc__ attribute belonging the function:

Access the DocString

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString16/20

● You can access the DocString via the __doc__ attribute belonging the function:

Access the DocString

● Note: each module, class or function has the __doc__ field added to it by python automatically. How can this be achieved? – modules, classes and function are themselves objects of classes that derive from base classes, see Lecture­06 and 07).

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString17/20

● The DocString is also retrieved from the built-in help() function of python. Try the following with the function given in this example:

● Save it to a file max.py● Execute it with python ­i max.py # this keeps python open after execution● In the prompt type help(max)

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The DocString17/20

● The DocString is also retrieved from the built-in help() function of python. Try the following with the function given in this example:

● Save it to a file max.py● Execute it with python ­i max.py # this keeps python open after execution● In the prompt type help(max)

You should see an output similar to this one:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 5:

Variable arguments

18/20

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable arguments19/20

● Python supports passing of an arbitrary number of arguments. These arguments are passed on to the function as tuple or dict:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable arguments19/20

atuple_of_args will be passed as a tuple

● Python supports passing of an arbitrary number of arguments. These arguments are passed on to the function as tuple or dict:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable arguments19/20

atuple_of_args will be passed as a tuple

adict_of_args will be passed as a dict

● Python supports passing of an arbitrary number of arguments. These arguments are passed on to the function as tuple or dict:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable arguments19/20

atuple_of_args will be passed as a tuple

adict_of_args will be passed as a dict

● This feature is known as VarArg parameters.

● Python supports passing of an arbitrary number of arguments. These arguments are passed on to the function as tuple or dict:

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable arguments19/20

atuple_of_args will be passed as a tuple

adict_of_args will be passed as a dict

● This feature is known as VarArg parameters.

● Python supports passing of an arbitrary number of arguments. These arguments are passed on to the function as tuple or dict:

● Sneak into Lecture­05 to learn all you need about tuples and dicts.

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Summary

● Functions in python.

20/20

● Global sv. local variables.

● Default arguments in functions.

● Keyword arguments in functions.

● The DocString.

● The lambda function.

● VarArgs.