69
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 05: Functions Roger Wolf 06. June 2019

An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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 05: Functions

Roger Wolf06. June 2019

Page 2: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 11:

The python function

1/25

Page 3: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Function definition2/25

● In python functions are defined as shown in the example below:

Page 4: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

2/25

● In python functions are defined as shown in the example below:

Defines a function.

Function definition

Page 5: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

2/25

● In python functions are defined as shown in the example below:

Defines a function.

Function name.

Function definition

Page 6: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

2/25

● In python functions are defined as shown in the example below:

Defines a function.

Function name.

Function arguments.

Function definition

Page 7: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

3/25

● In python functions are defined as shown in the example below:

Interface & implementation

Function interface to outside world/user.

Page 8: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

3/25

● In python functions are defined as shown in the example below:

Interface & implementation

Implementation of the function.

Function interface to outside world/user.

Page 9: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

3/25

● In python functions are defined as shown in the example below:

Interface & implementation

Implementation of the function.

Function interface to outside world/user.

Implementation of function ends here.

Page 10: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

3/25

● In python functions are defined as shown in the example below:

● The function opens its own range of validity (=statement block). I.e. everything indented after the function definition is part of the function.

Interface & implementation

Implementation of the function.

Function interface to outside world/user.

Implementation of function ends here.

Page 11: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

3/25

● In python functions are defined as shown in the example below:

● The function opens its own range of validity (=statement block). I.e. everything indented after the function definition is part of the function.

● Again note the importance of indentation in this case.

Interface & implementation

Implementation of the function.

Function interface to outside world/user.

Implementation of function ends here.

Page 12: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Function call4/25

● The following example shows how the function is called inside your script:

Page 13: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Function call4/25

● The following example shows how the function is called inside your script:

Here the function is called.

● Function arguments are given in braces, separated by comma.

Page 14: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Return value5/25

● A function may have a return value (while it does not have to):

Page 15: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Return value5/25

max(x,y) has a return value.

● A function may have a return value (while it does not have to):

● The return value is indicated by the key word return.

Page 16: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Return value5/25

max(x,y) has a return value.

● A function may have a return value (while it does not have to):

● The return value is indicated by the key word return.

● The return value is not type bound, i.e. it could be an int, float, string, tuple, list, … depending on the context (→ see the example above).

Page 17: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Return value5/25

max(x,y) has a return value.

● A function may have a return value (while it does not have to):

● The return value is indicated by the key word return.

● The return value is not type bound, i.e. it could be an int, float, string, tuple, list, … depending on the context (→ see the example above).

● If there is nothing returned the return value will be None.

Page 18: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments6/25

● The following is a dangerous example of using dynamic typing:

Page 19: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments6/25

● The following is a dangerous example of using dynamic typing:

The result is 6.

Page 20: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments6/25

● The following is a dangerous example of using dynamic typing:

The result is 6.

The result is aaa.

Page 21: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments6/25

● The following is a dangerous example of using dynamic typing:

The result is 6.

The result is aaa.

● As you see the function does different things depending on the function arguments at runtime. This is possible as long as an operator (here “*”) is defined for all types in use.

Page 22: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments7/25

● The following is a dangerous example of using dynamic typing:

● If you have to be sure about the type of your arguments you can always check it using the function type.

● As you see the function does different things depending on the function arguments at runtime. This is possible as long as an operator (here “*”) is defined for all types in use.

Page 23: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Types of function arguments7/25

● The following is a dangerous example of using dynamic typing:

● If you have to be sure about the type of your arguments you can always check it using the function type.

Checking the type of a variable exploiting the type function.

● As you see the function does different things depending on the function arguments at runtime. This is possible as long as an operator (here “*”) is defined for all types in use.

Page 24: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

8/25

● Tuples (of arbitrary length) can easily be passed to or returned from functions:

Tuples & functions

Page 25: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

8/25

● Tuples (of arbitrary length) can easily be passed to or returned from functions:

Tuples & functions

Expects two tuples of types (float, float).

Page 26: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

8/25

● Tuples (of arbitrary length) can easily be passed to or returned from functions:

Tuples & functions

Expects two tuples of types (float, float).

Also returns a tuple of type (float, float).

Page 28: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

The lambda function10/25

● You can define functions at runtime, which are not bound to a name. These functions are usually used to fulfill simple tasks:

Page 29: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

The lambda function10/25

● You can define functions at runtime, which are not bound to a name. These functions are usually used to fulfill simple tasks:

This is a normal (named) function.

Page 30: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

The lambda function10/25

● You can define functions at runtime, which are not bound to a name. These functions are usually used to fulfill simple tasks:

This is a normal (named) function.

This is a (nameless) lambda function, g is a pointer to this function.

Page 31: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

The lambda function10/25

● You can define functions at runtime, which are not bound to a name. These functions are usually used to 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 name nor function pointer. The first set of braces defines the function, the second set of braces passes the arguments.

Page 32: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Built-in (common) functions to be applied on sequences11/25

● You can apply several useful operations on all sequences, among those:

● len(seq):

Return the length of seq.

● filter(fn, seq):

Apply a function fn to each element of seq; fn should return a boolean; filter returns an iterator through the elements of seq for which fn is true.

● map(fn, seq):

Apply a function fn to each element of seq; fn gives a return value for each element; map returns an iterator through the elements of seq to which fn has been applied element-wise.

● reduce(fn, seq):

Apply a function to each element of seq; fn gives a rule how to combine two elements of seq; reduce returns a combination of all elements applying fn pair-wise up to the end of the sequence.

Page 33: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

● Task: Create a list of sorted objects that can be divided by three w/o rest class from an arbitrary input list:

12/25

Example for the built-in function filter

Page 34: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

● Task: Create a list of sorted objects that can be divided by three w/o rest class from an arbitrary input list:

12/25

Example for the built-in function filter

● The function filter is used with a lambda function that returns a boolean.

● The return value of filter is an iterator through the elements in foo, for which the lambda function returns true.

● To turn these into a new list we use list comprehension.

Page 35: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

13/25

Example for the built-in function map

● Task: Create a dictionary from the input, with all string values in lower case:

Page 36: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

13/25

Example for the built-in function map

● Task: Create a dictionary from the input, with all string values in lower case:

Iterator to the key value pairs in moo.

Page 37: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

13/25

Example for the built-in function map

Iterator to the key value pairs in moo.

Lambda function takes key value pair as input & returns a key value pair w/ string value in lower case.

● Task: Create a dictionary from the input, with all string values in lower case:

Page 38: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

13/25

Example for the built-in function map

Iterator to the key value pairs in moo.

Lambda function takes key value pair as input & returns a key value pair w/ string value in lower case.Access actual key value

pair from iterator.

● Task: Create a dictionary from the input, with all string values in lower case:

Page 39: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

13/25

Example for the built-in function map

Iterator to the key value pairs in moo.

Lambda function takes key value pair as input & returns a key value pair w/ string value in lower case.Access actual key value

pair from iterator.

● Note: This is not the most elegant way of doing this…

● Task: Create a dictionary from the input, with all string values in lower case:

Page 40: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

14/25

Example for the built-in function map

● Note: This is not the most elegant way of doing this…

Thinking python: … doing a very similar thing in one line of code. Can you decipher what is being done here? Can you see the difference in output to what has been discussed before?

● Task: Create a dictionary from the input, with all string values in lower case:

Page 41: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

● Task: Calculate the magnitude of a vector of any dimension, represented by a tuple.

15/25

Example for the common function reduce

Page 42: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

● Task: Calculate the magnitude of a vector of any dimension, represented by a tuple.

15/25

Example for the common function reduce

Example for a lambda function w/ two arg’s.

Page 43: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

● Task: Calculate the magnitude of a vector of any dimension, represented by a tuple.

15/25

Example for the common function reduce

Example for a lambda function w/ two arg’s.

● The lambda function gives the rule, how to combine two elements of the sequence, pairwise. In this case the return value of reduce is a single number, to which the sequence has been reduced.

Page 44: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 13:

Global vs. local variables

16/25

Page 45: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables17/25

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

Page 46: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables17/25

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

The value of each function argument is copied into x here.

Page 47: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables17/25

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

The value of each function argument is copied into x here.

x changes its value to 5 here.

Page 48: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables17/25

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

The value of each function argument is copied into x here.

x changes its value to 5 here.

But glob kept its initial value.

Page 49: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables17/25

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

The value of each function argument is copied into x here.

x changes its value to 5 here.

But glob kept its initial value.

Page 50: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Global vs. local variables18/25

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

The value of each function argument is copied into x here.

x changes its value to 5 here.

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

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

Page 51: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Side remark19/25

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

● The statement at the bottom does not apply to the scope of if statements, as the following example shows:

Page 52: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Side remark19/25

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

j defined inside statement block of if statement.

● The statement at the bottom does not apply to the scope of if statements, as the following example shows:

Page 53: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Side remark19/25

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

j defined inside statement block of if statement.

● The statement at the bottom does not apply to the scope of if statements, as the following example shows:

But still accessible outside.

Page 54: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Side remark19/25

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

j defined inside statement block of if statement.

● The statement at the bottom does not apply to the scope of if statements, as the following example shows:

But still accessible outside.

Page 55: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Side remark19/25

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

j defined inside statement block of if statement.

● The statement at the bottom does not apply to the scope of if statements, as the following example shows:

But still accessible outside.

Try the same for i=0.

Page 56: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions20/25

● Task: Try to access/modify a global variable inside a function:

Trial-1: – the naive approach –

Page 57: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions20/25

● Task: Try to access/modify a global variable inside a function:

Trial-1: – the naive approach –

Access glob, which is defined in the scope above fn.

Page 58: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions20/25

● Task: Try to access/modify a global variable inside a function:

Trial-1: – the naive approach –

Access glob, which is defined in the scope above fn.

Page 59: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions21/25

● Task: Try to access/modify a global variable inside a function:

Trial-2: – pass as function argument –

Page 60: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions21/25

● Task: Try to access/modify a global variable inside a function:

Pass as function argument here.

Trial-2: – pass as function argument –

Page 61: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions21/25

● Task: Try to access/modify a global variable inside a function:

Pass as function argument here.

Trial-2: – pass as function argument –

Page 62: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions22/25

● Task: Try to access/modify a global variable inside a function:

Trial-3: – using the global declaration –

Page 63: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions22/25

● Task: Try to access/modify a global variable inside a function:

Make glob known as global variable inside fn using the key word global.

Trial-3: – using the global declaration –

Page 64: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions22/25

● Task: Try to access/modify a global variable inside a function:

Make glob known as global variable inside fn using the key word global.

Trial-3: – using the global declaration –

Page 65: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions22/25

● Task: Try to access/modify a global variable inside a function:

Make glob known as global variable inside fn using the key word global.

Trial-3: – using the global declaration –

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

Page 66: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

References to global variables in functions23/25

● Task: Try to access/modify a global variable inside a function:

Change glob upon return value of fn.

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

Page 67: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

A word on the global declaration24/25

● If you can copy variables into function arguments and back via return values, what do you need the global declaration for at all?

Page 68: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

A word on the global declaration24/25

● If you can copy variables into function arguments and back via return values, what do you need the global declaration for at all?

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

Page 69: An Introduction to python - ekprwolf/teaching/courses/Python201… · In python functions are defined as shown in the example below: The function opens its own range of validity (=statement

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

Summary25/25

● Part 11: The python function.

● Part 12: Lambda functions.

● Part 13: Global vs. local variables.