an introduction to python lecture 03: functionsekprwolf/teaching/... · note: this is in contrast...

63
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 Wolf 23. Mai 2018

Upload: others

Post on 12-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 2: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 3: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 4: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 5: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 6: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 7: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 8: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 9: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 10: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 11: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Checking types3/20

● Still python safes type information that can be checked:

Page 12: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 13: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 14: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 15: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 16: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 17: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 18: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 19: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 20: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 21: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 22: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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().

Page 23: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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.

Page 24: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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.

Page 25: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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 –

Page 26: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 27: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 28: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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 –

Page 29: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 30: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 31: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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 –

Page 32: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 33: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 34: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 35: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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()

Page 36: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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.

Page 37: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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).

Page 38: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 39: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Default arguments12/20

● You can have default arguments for a function:

Page 40: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 41: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 42: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 43: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 44: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 45: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 46: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 47: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 48: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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):

Page 49: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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…

Page 50: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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)

Page 51: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 52: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 53: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 54: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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).

Page 55: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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)

Page 56: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 57: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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

Page 58: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 59: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 60: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 61: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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:

Page 62: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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.

Page 63: An Introduction to python Lecture 03: Functionsekprwolf/teaching/... · Note: This is in contrast to C++, which is statically typing (see Lecture01). Something like function overloading,

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.