Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP...

22
Lilian Blot Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1

Transcript of Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP...

Page 1: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian BlotLilian Blot

CORE ELEMENTS PART V: FUNCTIONS

PARAMETERS&

VARIABLES SCOPE

Lecture 5

Autumn 2014TPOP

1

Page 2: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Overview

Function Parameters

Function: reading the small prints

Variable Scope

Autumn 2014TPOP

2

Page 3: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Passing Parameters

Code in Python interpreter File: TPOP_2014_15_Lecture5_parameterPassing.py

Autumn 2014TPOP

3

Page 4: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Passing Parameters

Autumn 2014TPOP

4

account

rate

my_account

bank_rate

100.0

0.07

107.0

def addInterestOne(account, rate): account = account * (1+rate)

my_account = 100.0bank_rate = 0.07addInterestOne(my_account, bank_rate)print "new accounts balance:", my_account

Code

Page 5: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Passing Parameters

Autumn 2014TPOP

5

accounts

rate

lst_account

bank_rate0.07

def addInterestAll(accounts, rate): for account in range(len(accounts)): accounts[account] *= (1+rate)

lst_accounts = [10, 20, 100]bank_rate = 0.07addInterestAll(lst_accounts, bank_rate)print "new accounts balance:", lst_account

Code

0 1 2

10 20 10010.7 21.4 107

account0

1

2

Page 6: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Passing Parameters

Autumn 2014TPOP

6

account

rate

my_account

bank_rate

100.0

0.07

107.0

def addInterestOne(account, rate): account = account * (1+rate) return account

my_account = 100.0bank_rate = 0.07my_account = addInterestOne(my_account, bank_rate)print "new accounts balance:", my_account

Code

Page 7: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Function Design Concepts

Use arguments for inputs and return for outputs

Use global variables only when absolutely necessary

Do NOT change mutable arguments unless the caller expect it

Autumn 2014TPOP

7

Page 8: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian BlotLilian Blot

THE SMALL PRINTS

Functions

Autumn 2014TPOP

8

Page 9: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Variable Scope

Code in Python interpreter File: TPOP_2014_15_Lecture5_python_scope.py

Autumn 2014TPOP

9

Page 10: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Namespace

In order to avoid clashes between names (variables) inside the function and outside the function, function define a nested namespace

Functions define local scopes Modules define global scopes Each call to a function is a new local scope Assigned names in a function are local, unless

declared global Names not assigned a value in the definition function

are assumed to be global

Autumn 2014TPOP

10

Page 11: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Namespace & Variable Scope

Python’s three scopes

Autumn 2014TPOP

11

Built-in• predefined names: len, max, …

Global (module)• Names assigned at top level of a module• Names declared “global” in function

Local (function)• Names assigned inside a function

def

Page 12: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Name Resolution: LGB rule

Name/variable references search at most 3 scopes:

Local Global Built-in

Autumn 2014TPOP

12

Page 13: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Namespace & Variable Scope

Python’s three scopes

Autumn 2014TPOP

13

Built-in• predefined names: len, max, …

Global (module)• Names assigned at top level of a module

• my_account• bank_rate

• Names declared “global” in function

Local (function) addInterestOne• account• rate

bank

Math moduleGlobal name:• pi• e

Local (function) sqrt

Local (function) exp

math

Page 14: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Name Resolution: LGB rule

When you use an unqualified name inside a function, Python searches the local (L), then the global (G), and then the built-in (B) scopes and stop at the first place the name is found

see change_global1(x) and change_global2(x) in python_scope.py

Autumn 2014TPOP

14

Page 15: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Name Resolution: LGB rule

When you assign a name in a function (e.g. result = 1.0), Python always creates or change the name in the local scope, unless it’s declared to be global in that function.

see change_global4(x) and change_global3(x) in python_scope.py

Autumn 2014TPOP

15

Page 16: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Name Resolution: LGB rule

When outside a function, the local scope is the same as the global, e.g. the module’s namespace.

Autumn 2014TPOP

16

Page 17: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Name Resolution: LGB rule

To summarise:

Global names are ??? Local names are ???

Autumn 2014TPOP

17

x, y, v = 1, 3, 7

def the_global_thing(z): global u v = 5 u = x + y * (z – v)

Code

Page 18: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Summary

Parameter passing Immutable object passed by value Mutable object passed by reference

Namespace and scopes Three scopes Local Global Built-in

Autumn 2014TPOP

18

Page 19: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Exercises

Try to understand the scope of each object and what is really happening in the code provided in the file:

TPOP_2014_15_Lecture5_python_scope_exercises.py

Compare and Discuss your findings with one of your peers.

Autumn 2014TPOP

19

Page 20: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

More on Function

Warning

You do not need to know or understand this right now. You may not even use it this year

READ the following slide ONLY if you are confident with what we have seen so far on

function

Autumn 2014TPOP

20

Page 21: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Special Argument-Matching Modes

Positional: matched left to right What we have seen so far

Keywords: matched by argument name

Autumn 2014TPOP

21

def my_func(name, age = 18, nationality = ‘French’): print name, age, nationality

my_func(‘Lilian Blot’, age = 21)my_func(name = ‘toto’, nationality = ‘UK’)my_func(‘titi’, 5, ‘US’)my_func(‘nono’, 30)

Code

Page 22: Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.

Lilian Blot

Special Argument-Matching Modes

Defaults: specify values for arguments that are not passed

varargs: catch unmatched positional or keyword arguments

Autumn 2014TPOP

22

def my_args_func(*args): #unmatched positional argument print args

def my_args2_func(**args): # unmatched keyword argument print args

Code