Python 
Functions 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.
A programming language should not include 
everything anyone might ever want 
Python Functions
A programming language should not include 
everything anyone might ever want 
Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee 
what they need to solve specific problems 
Python Functions
A programming language should not include 
everything anyone might ever want 
Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee 
what they need to solve specific problems 
Define functions to create higher-level operations 
Python Functions
A programming language should not include 
everything anyone might ever want 
Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee 
what they need to solve specific problems 
Define functions to create higher-level operations 
"Create a language in which the solution to your 
original problem is trivial." 
Python Functions
Define functions using def 
Python Functions
Define functions using def 
ddddeeeeffff greet(): 
rrrreeeettttuuuurrrrnnnn 'Good evening, master' 
Python Functions
Define functions using def 
ddddeeeeffff greet(): 
rrrreeeettttuuuurrrrnnnn 'Good evening, master' 
temp = greet() 
pppprrrriiiinnnntttt temp 
Good evening, master 
Python Functions
Give them parameters 
Python Functions
Give them parameters 
ddddeeeeffff greet(name): 
answer = 'Hello, ' + name 
rrrreeeettttuuuurrrrnnnn answer 
Python Functions
Give them parameters 
ddddeeeeffff greet(name): 
answer = 'Hello, ' + name 
temp 
'doctor' 
rrrreeeettttuuuurrrrnnnn answer 
temp = 'doctor' 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Give them parameters 
ddddeeeeffff greet(name): 
answer = 'Hello, ' + name 
temp 
'doctor' 
name 
rrrreeeettttuuuurrrrnnnn answer 
temp = 'doctor' 
result = greet(temp) 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Give them parameters 
ddddeeeeffff greet(name): 
answer = 'Hello, ' + name 
temp 
'doctor' 
'Hello, doctor' 
name 
answer 
rrrreeeettttuuuurrrrnnnn answer 
temp = 'doctor' 
result = greet(temp) 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Give them parameters 
ddddeeeeffff greet(name): 
answer = 'Hello, ' + name 
temp 
'doctor' 
'Hello, doctor' 
rrrreeeettttuuuurrrrnnnn answer 
temp = 'doctor' 
result = greet(temp) 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
val 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
c 
val 
ddddoooouuuubbbblllleeee 
result = double(val) 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
a 
c 
val 
ddddoooouuuubbbblllleeee aaaadddddddd 
result = double(val) 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
11 
a 
b 
c 
val 
ddddoooouuuubbbblllleeee aaaadddddddd 
result = double(val) 
pppprrrriiiinnnntttt result 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
22 
c 
d 
val 
ddddoooouuuubbbblllleeee 
result = double(val) 
pppprrrriiiinnnntttt result 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
22 
val 
result = double(val) 
pppprrrriiiinnnntttt result 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Each function call creates a new stack frame 
ddddeeeeffff add(a): 
b = a + 1 
rrrreeeettttuuuurrrrnnnn b 
ddddeeeeffff double(c): 
d = 2 * add(c) 
rrrreeeettttuuuurrrrnnnn d 
val = 10 
10 
22 
val 
result = double(val) 
pppprrrriiiinnnntttt result 
22 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Only see variables in the current and global frames 
Python Functions
Only see variables in the current and global frames 
Current beats global 
Python Functions
Only see variables in the current and global frames 
Current beats global 
ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: 
temp = 'Hello, ' + name 
rrrreeeettttuuuurrrrnnnn temp 
temp = 'doctor' 
result = greet(temp) 
Python Functions
Only see variables in the current and global frames 
Current beats global 
ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: 
temp = 'Hello, ' + name 
rrrreeeettttuuuurrrrnnnn temp 
temp = 'doctor' 
result = greet(temp) temp 
'Hello, doctor' 
'doctor' 
gggglllloooobbbbaaaallll 
ggggrrrreeeeeeeetttt 
name 
temp 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Only see variables in the current and global frames 
Current beats global 
ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: 
temp = 'Hello, ' + name 
rrrreeeettttuuuurrrrnnnn temp 
temp = 'doctor' 
result = greet(temp) 
pppprrrriiiinnnntttt result 
temp 
'Hello, doctor' 
'doctor' 
gggglllloooobbbbaaaallll 
Hello, doctor 
result 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Can pass values in and accept results directly 
Python Functions
Can pass values in and accept results directly 
ddddeeeeffff greet(name): 
rrrreeeettttuuuurrrrnnnn 'Hello, ' + name 
pppprrrriiiinnnntttt greet('doctor') 
Python Functions
Can pass values in and accept results directly 
ddddeeeeffff greet(name): 
rrrreeeettttuuuurrrrnnnn 'Hello, ' + name 
pppprrrriiiinnnntttt greet('doctor') 
_x1_ 
'doctor' 
'Hello, doctor' 
name 
_x2_ 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Can return at any time 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
pppprrrriiiinnnntttt sign(-9) 
-1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
Over-use makes functions 
rrrreeeettttuuuurrrrnnnn 1 hard to understand 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
pppprrrriiiinnnntttt sign(-9) 
-1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
Over-use makes functions 
rrrreeeettttuuuurrrrnnnn 1 hard to understand 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
No prescription possible, but: 
1 
pppprrrriiiinnnntttt sign(-9) 
-1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
Over-use makes functions 
rrrreeeettttuuuurrrrnnnn 1 hard to understand 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
No prescription possible, but: 
– a few at the beginning 
to handle special cases 
1 
pppprrrriiiinnnntttt sign(-9) 
-1 
Python Functions
Can return at any time 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
Over-use makes functions 
rrrreeeettttuuuurrrrnnnn 1 hard to understand 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
eeeellllsssseeee:::: 
rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
No prescription possible, but: 
– a few at the beginning 
to handle special cases 
– one at the end for the 
1 
pppprrrriiiinnnntttt sign(-9) 
-1 
"general" result 
Python Functions
Every function returns something 
Python Functions
Every function returns something 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
# eeeellllsssseeee:::: 
# rrrreeeettttuuuurrrrnnnn -1 
Python Functions
Every function returns something 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
# eeeellllsssseeee:::: 
# rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
Python Functions
Every function returns something 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
rrrreeeettttuuuurrrrnnnn 1 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
# eeeellllsssseeee:::: 
# rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
pppprrrriiiinnnntttt sign(-9) 
None 
Python Functions
Every function returns something 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
If the function doesn't return 
rrrreeeettttuuuurrrrnnnn 1 a value, Python returns None 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
# eeeellllsssseeee:::: 
# rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
1 
pppprrrriiiinnnntttt sign(-9) 
None 
Python Functions
Every function returns something 
ddddeeeeffff sign(num): 
iiiiffff num > 0: 
If the function doesn't return 
rrrreeeettttuuuurrrrnnnn 1 a value, Python returns None 
eeeelllliiiiffff num == 0: 
rrrreeeettttuuuurrrrnnnn 0 
# eeeellllsssseeee:::: 
# rrrreeeettttuuuurrrrnnnn -1 
pppprrrriiiinnnntttt sign(3) 
Yet another reason why 
commenting out blocks of code 
is a bad idea... 
1 
pppprrrriiiinnnntttt sign(-9) 
None 
Python Functions
Functions and parameters don't have types 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
pppprrrriiiinnnntttt double(2) 
4 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
pppprrrriiiinnnntttt double(2) 
4 
pppprrrriiiinnnntttt double('two') 
twotwo 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
Only use this when the 
function's behavior depends 
pppprrrriiiinnnntttt double(2) 
4 
pppprrrriiiinnnntttt double('two') 
twotwo 
only on properties that all 
possible arguments share 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
Only use this when the 
function's behavior depends 
pppprrrriiiinnnntttt double(2) 
4 
pppprrrriiiinnnntttt double('two') 
twotwo 
only on properties that all 
possible arguments share 
iiiiffff type(arg) == int: 
... 
eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: 
... 
... 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
Only use this when the 
function's behavior depends 
pppprrrriiiinnnntttt double(2) 
4 
pppprrrriiiinnnntttt double('two') 
twotwo 
only on properties that all 
possible arguments share 
Warning sign iiiiffff type(arg) == int: 
... 
eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: 
... 
... 
Python Functions
Functions and parameters don't have types 
ddddeeeeffff double(x): 
rrrreeeettttuuuurrrrnnnn 2 * x 
Only use this when the 
function's behavior depends 
pppprrrriiiinnnntttt double(2) 
4 
pppprrrriiiinnnntttt double('two') 
twotwo 
only on properties that all 
possible arguments share 
Warning sign 
iiiiffff type(arg) == int: 
There's a better 
way to do this 
... 
eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: 
... 
... 
Python Functions
Values are copied into parameters 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
string_val 
'alan' 
['alan'] 
list_val 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
string_val 
'alan' 
['alan'] 
a_string 
a_list 
list_val 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
string_val 
'alan' 
'alanturing' 
['alan'] 
a_string 
a_list 
list_val 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
string_val 
'alan' 
'alanturing' 
['alan', 
a_string 
a_list 
list_val 'turing'] 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Values are copied into parameters 
Which means lists are aliased 
ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: 
a_string += 'turing' 
a_list.append('turing') 
string_val = 'alan' 
list_val = ['alan'] 
appender(string_val, list_val) 
pppprrrriiiinnnntttt string_val 
string_val 
'alan' 
['alan', 
alan 
pppprrrriiiinnnntttt list_val 
['alan', 'turing'] 
list_val 'turing'] 
ssssttttaaaacccckkkk vvvvaaaalllluuuueeee 
Python Functions
Can define default parameter values 
Python Functions
Can define default parameter values 
ddddeeeeffff adjust(value, amount=2.0): 
rrrreeeettttuuuurrrrnnnn value * amount 
Python Functions
Can define default parameter values 
ddddeeeeffff adjust(value, amount=2.0): 
rrrreeeettttuuuurrrrnnnn value * amount 
pppprrrriiiinnnntttt adjust(5) 
10 
Python Functions
Can define default parameter values 
ddddeeeeffff adjust(value, amount=2.0): 
rrrreeeettttuuuurrrrnnnn value * amount 
pppprrrriiiinnnntttt adjust(5) 
10 
pppprrrriiiinnnntttt adjust(5, 1.001) 
5.005 
Python Functions
More readable than multiple functions 
Python Functions
More readable than multiple functions 
ddddeeeeffff adjust_general(value, amount): 
rrrreeeettttuuuurrrrnnnn value * amount 
ddddeeeeffff adjust_default(value): 
rrrreeeettttuuuurrrrnnnn adjust_general(value, 2.0) 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
ddddeeeeffff triplet(left='venus', middle, right='mars'): 
rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
ddddeeeeffff triplet(left='venus', middle, right='mars'): 
rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) 
pppprrrriiiinnnntttt triplet('earth') 
venus earth mars 
OK so far... 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
ddddeeeeffff triplet(left='venus', middle, right='mars'): 
rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) 
pppprrrriiiinnnntttt triplet('earth') 
venus earth mars 
pppprrrriiiinnnntttt triplet('pluto', 'earth') 
OK so far... 
? 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
ddddeeeeffff triplet(left='venus', middle, right='mars'): 
rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) 
pppprrrriiiinnnntttt triplet('earth') 
venus earth mars 
pppprrrriiiinnnntttt triplet('pluto', 'earth') 
OK so far... 
? 
triplet('pluto', 'earth', 'mars') 
Python Functions
Parameters that have defaults must come after 
parameters that do not 
ddddeeeeffff triplet(left='venus', middle, right='mars'): 
rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) 
pppprrrriiiinnnntttt triplet('earth') 
venus earth mars 
pppprrrriiiinnnntttt triplet('pluto', 'earth') 
OK so far... 
? 
triplet('pluto', 'earth', 'mars') 
triplet('venus', 'pluto', 'earth') 
Python Functions
"When should I write a function?" 
Python Functions
"When should I write a function?" 
Human short term memory can hold 7± 2 items 
Python Functions
"When should I write a function?" 
Human short term memory can hold 7± 2 items 
If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss 
in their mind at once to understand a block of code, 
it's too long 
Python Functions
"When should I write a function?" 
Human short term memory can hold 7± 2 items 
If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss 
in their mind at once to understand a block of code, 
it's too long 
Break it into comprehensible pieces with functions 
Python Functions
"When should I write a function?" 
Human short term memory can hold 7± 2 items 
If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss 
in their mind at once to understand a block of code, 
it's too long 
Break it into comprehensible pieces with functions 
Even if each function is only called once 
Python Functions
Example 
ffffoooorrrr x iiiinnnn range(1, GRID_WIDTH-1): 
ffffoooorrrr y iiiinnnn range(1, GRID_HEIGHT-1): 
iiiiffff (density[x-1][y] > density_threshold) or  
(density[x+1][y] > density_threshold): 
iiiiffff (flow[x][y-1] < flow_threshold) or 
(flow[x][y+1] < flow_threshold): 
temp = (density[x-1][y] + density[x+1][y]) / 2 
iiiiffff abs(temp - density[x][y]) > update_threshold: 
density[x][y] = temp 
Python Functions
Refactoring #1: grid interior 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff (density[x-1][y] > density_threshold) or  
(density[x+1][y] > density_threshold): 
iiiiffff (flow[x][y-1] > flow_threshold) or 
(flow[x][y+1] > flow_threshold): 
temp = (density[x-1][y] + density[x+1][y]) / 2 
iiiiffff abs(temp - density[x][y]) > update_threshold: 
density[x][y] = temp 
Python Functions
Refactoring #2: tests on X and Y axes 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff density_exceeds(density, x, y, density_threshold): 
iiiiffff flow_exceeds(flow, x, y, flow_threshold): 
temp = (density[x-1][y] + density[x+1][y]) / 2 
iiiiffff abs(temp - density[x][y]) > tolerance: 
density[x][y] = temp 
Python Functions
Refactoring #3: update rule 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff density_exceeds(density, x, y, density_threshold): 
iiiiffff flow_exceeds(flow, x, y, flow_threshold): 
update_on_tolerance(density, x, y, tolerance) 
Python Functions
Refactoring #3: update rule 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff density_exceeds(density, x, y, density_threshold): 
iiiiffff flow_exceeds(flow, x, y, flow_threshold): 
update_on_tolerance(density, x, y, tolerance) 
Good programmers will write this first 
Python Functions
Refactoring #3: update rule 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff density_exceeds(density, x, y, density_threshold): 
iiiiffff flow_exceeds(flow, x, y, flow_threshold): 
update_on_tolerance(density, x, y, tolerance) 
Good programmers will write this first 
Then write the functions it implies 
Python Functions
Refactoring #3: update rule 
ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): 
ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): 
iiiiffff density_exceeds(density, x, y, density_threshold): 
iiiiffff flow_exceeds(flow, x, y, flow_threshold): 
update_on_tolerance(density, x, y, tolerance) 
Good programmers will write this first 
Then write the functions it implies 
Then refactor any overlap 
Python Functions
created by 
Greg Wilson 
October 2010 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.

Functions

  • 1.
    Python Functions Copyright© Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
  • 2.
    A programming languageshould not include everything anyone might ever want Python Functions
  • 3.
    A programming languageshould not include everything anyone might ever want Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee what they need to solve specific problems Python Functions
  • 4.
    A programming languageshould not include everything anyone might ever want Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee what they need to solve specific problems Define functions to create higher-level operations Python Functions
  • 5.
    A programming languageshould not include everything anyone might ever want Instead, it should make it easy for ppeeooppllee ttoo ccrreeaattee what they need to solve specific problems Define functions to create higher-level operations "Create a language in which the solution to your original problem is trivial." Python Functions
  • 6.
    Define functions usingdef Python Functions
  • 7.
    Define functions usingdef ddddeeeeffff greet(): rrrreeeettttuuuurrrrnnnn 'Good evening, master' Python Functions
  • 8.
    Define functions usingdef ddddeeeeffff greet(): rrrreeeettttuuuurrrrnnnn 'Good evening, master' temp = greet() pppprrrriiiinnnntttt temp Good evening, master Python Functions
  • 9.
    Give them parameters Python Functions
  • 10.
    Give them parameters ddddeeeeffff greet(name): answer = 'Hello, ' + name rrrreeeettttuuuurrrrnnnn answer Python Functions
  • 11.
    Give them parameters ddddeeeeffff greet(name): answer = 'Hello, ' + name temp 'doctor' rrrreeeettttuuuurrrrnnnn answer temp = 'doctor' ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 12.
    Give them parameters ddddeeeeffff greet(name): answer = 'Hello, ' + name temp 'doctor' name rrrreeeettttuuuurrrrnnnn answer temp = 'doctor' result = greet(temp) ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 13.
    Give them parameters ddddeeeeffff greet(name): answer = 'Hello, ' + name temp 'doctor' 'Hello, doctor' name answer rrrreeeettttuuuurrrrnnnn answer temp = 'doctor' result = greet(temp) ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 14.
    Give them parameters ddddeeeeffff greet(name): answer = 'Hello, ' + name temp 'doctor' 'Hello, doctor' rrrreeeettttuuuurrrrnnnn answer temp = 'doctor' result = greet(temp) result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 15.
    Each function callcreates a new stack frame Python Functions
  • 16.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 17.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 val ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 18.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 c val ddddoooouuuubbbblllleeee result = double(val) ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 19.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 a c val ddddoooouuuubbbblllleeee aaaadddddddd result = double(val) ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 20.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 11 a b c val ddddoooouuuubbbblllleeee aaaadddddddd result = double(val) pppprrrriiiinnnntttt result result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 21.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 22 c d val ddddoooouuuubbbblllleeee result = double(val) pppprrrriiiinnnntttt result result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 22.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 22 val result = double(val) pppprrrriiiinnnntttt result result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 23.
    Each function callcreates a new stack frame ddddeeeeffff add(a): b = a + 1 rrrreeeettttuuuurrrrnnnn b ddddeeeeffff double(c): d = 2 * add(c) rrrreeeettttuuuurrrrnnnn d val = 10 10 22 val result = double(val) pppprrrriiiinnnntttt result 22 result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 24.
    Only see variablesin the current and global frames Python Functions
  • 25.
    Only see variablesin the current and global frames Current beats global Python Functions
  • 26.
    Only see variablesin the current and global frames Current beats global ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: temp = 'Hello, ' + name rrrreeeettttuuuurrrrnnnn temp temp = 'doctor' result = greet(temp) Python Functions
  • 27.
    Only see variablesin the current and global frames Current beats global ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: temp = 'Hello, ' + name rrrreeeettttuuuurrrrnnnn temp temp = 'doctor' result = greet(temp) temp 'Hello, doctor' 'doctor' gggglllloooobbbbaaaallll ggggrrrreeeeeeeetttt name temp ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 28.
    Only see variablesin the current and global frames Current beats global ddddddddeeeeeeeeffffffff ggrreeeett((nnaammee)):: temp = 'Hello, ' + name rrrreeeettttuuuurrrrnnnn temp temp = 'doctor' result = greet(temp) pppprrrriiiinnnntttt result temp 'Hello, doctor' 'doctor' gggglllloooobbbbaaaallll Hello, doctor result ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 29.
    Can pass valuesin and accept results directly Python Functions
  • 30.
    Can pass valuesin and accept results directly ddddeeeeffff greet(name): rrrreeeettttuuuurrrrnnnn 'Hello, ' + name pppprrrriiiinnnntttt greet('doctor') Python Functions
  • 31.
    Can pass valuesin and accept results directly ddddeeeeffff greet(name): rrrreeeettttuuuurrrrnnnn 'Hello, ' + name pppprrrriiiinnnntttt greet('doctor') _x1_ 'doctor' 'Hello, doctor' name _x2_ ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 32.
    Can return atany time Python Functions
  • 33.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 Python Functions
  • 34.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 Python Functions
  • 35.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 pppprrrriiiinnnntttt sign(-9) -1 Python Functions
  • 36.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: Over-use makes functions rrrreeeettttuuuurrrrnnnn 1 hard to understand eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 pppprrrriiiinnnntttt sign(-9) -1 Python Functions
  • 37.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: Over-use makes functions rrrreeeettttuuuurrrrnnnn 1 hard to understand eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) No prescription possible, but: 1 pppprrrriiiinnnntttt sign(-9) -1 Python Functions
  • 38.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: Over-use makes functions rrrreeeettttuuuurrrrnnnn 1 hard to understand eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) No prescription possible, but: – a few at the beginning to handle special cases 1 pppprrrriiiinnnntttt sign(-9) -1 Python Functions
  • 39.
    Can return atany time ddddeeeeffff sign(num): iiiiffff num > 0: Over-use makes functions rrrreeeettttuuuurrrrnnnn 1 hard to understand eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 eeeellllsssseeee:::: rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) No prescription possible, but: – a few at the beginning to handle special cases – one at the end for the 1 pppprrrriiiinnnntttt sign(-9) -1 "general" result Python Functions
  • 40.
    Every function returnssomething Python Functions
  • 41.
    Every function returnssomething ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 # eeeellllsssseeee:::: # rrrreeeettttuuuurrrrnnnn -1 Python Functions
  • 42.
    Every function returnssomething ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 # eeeellllsssseeee:::: # rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 Python Functions
  • 43.
    Every function returnssomething ddddeeeeffff sign(num): iiiiffff num > 0: rrrreeeettttuuuurrrrnnnn 1 eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 # eeeellllsssseeee:::: # rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 pppprrrriiiinnnntttt sign(-9) None Python Functions
  • 44.
    Every function returnssomething ddddeeeeffff sign(num): iiiiffff num > 0: If the function doesn't return rrrreeeettttuuuurrrrnnnn 1 a value, Python returns None eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 # eeeellllsssseeee:::: # rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) 1 pppprrrriiiinnnntttt sign(-9) None Python Functions
  • 45.
    Every function returnssomething ddddeeeeffff sign(num): iiiiffff num > 0: If the function doesn't return rrrreeeettttuuuurrrrnnnn 1 a value, Python returns None eeeelllliiiiffff num == 0: rrrreeeettttuuuurrrrnnnn 0 # eeeellllsssseeee:::: # rrrreeeettttuuuurrrrnnnn -1 pppprrrriiiinnnntttt sign(3) Yet another reason why commenting out blocks of code is a bad idea... 1 pppprrrriiiinnnntttt sign(-9) None Python Functions
  • 46.
    Functions and parametersdon't have types Python Functions
  • 47.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x Python Functions
  • 48.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x pppprrrriiiinnnntttt double(2) 4 Python Functions
  • 49.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x pppprrrriiiinnnntttt double(2) 4 pppprrrriiiinnnntttt double('two') twotwo Python Functions
  • 50.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x Only use this when the function's behavior depends pppprrrriiiinnnntttt double(2) 4 pppprrrriiiinnnntttt double('two') twotwo only on properties that all possible arguments share Python Functions
  • 51.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x Only use this when the function's behavior depends pppprrrriiiinnnntttt double(2) 4 pppprrrriiiinnnntttt double('two') twotwo only on properties that all possible arguments share iiiiffff type(arg) == int: ... eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: ... ... Python Functions
  • 52.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x Only use this when the function's behavior depends pppprrrriiiinnnntttt double(2) 4 pppprrrriiiinnnntttt double('two') twotwo only on properties that all possible arguments share Warning sign iiiiffff type(arg) == int: ... eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: ... ... Python Functions
  • 53.
    Functions and parametersdon't have types ddddeeeeffff double(x): rrrreeeettttuuuurrrrnnnn 2 * x Only use this when the function's behavior depends pppprrrriiiinnnntttt double(2) 4 pppprrrriiiinnnntttt double('two') twotwo only on properties that all possible arguments share Warning sign iiiiffff type(arg) == int: There's a better way to do this ... eeeelllliiiiffff ttttyyyyppppeeee(arg) == str: ... ... Python Functions
  • 54.
    Values are copiedinto parameters Python Functions
  • 55.
    Values are copiedinto parameters Which means lists are aliased Python Functions
  • 56.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') Python Functions
  • 57.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) Python Functions
  • 58.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) string_val 'alan' ['alan'] list_val ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 59.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) string_val 'alan' ['alan'] a_string a_list list_val ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 60.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) string_val 'alan' 'alanturing' ['alan'] a_string a_list list_val ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 61.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) string_val 'alan' 'alanturing' ['alan', a_string a_list list_val 'turing'] ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 62.
    Values are copiedinto parameters Which means lists are aliased ddddddddeeeeeeeeffffffff aappppeennddeerr((aa__ssttrriinngg,, aa__lliisstt)):: a_string += 'turing' a_list.append('turing') string_val = 'alan' list_val = ['alan'] appender(string_val, list_val) pppprrrriiiinnnntttt string_val string_val 'alan' ['alan', alan pppprrrriiiinnnntttt list_val ['alan', 'turing'] list_val 'turing'] ssssttttaaaacccckkkk vvvvaaaalllluuuueeee Python Functions
  • 63.
    Can define defaultparameter values Python Functions
  • 64.
    Can define defaultparameter values ddddeeeeffff adjust(value, amount=2.0): rrrreeeettttuuuurrrrnnnn value * amount Python Functions
  • 65.
    Can define defaultparameter values ddddeeeeffff adjust(value, amount=2.0): rrrreeeettttuuuurrrrnnnn value * amount pppprrrriiiinnnntttt adjust(5) 10 Python Functions
  • 66.
    Can define defaultparameter values ddddeeeeffff adjust(value, amount=2.0): rrrreeeettttuuuurrrrnnnn value * amount pppprrrriiiinnnntttt adjust(5) 10 pppprrrriiiinnnntttt adjust(5, 1.001) 5.005 Python Functions
  • 67.
    More readable thanmultiple functions Python Functions
  • 68.
    More readable thanmultiple functions ddddeeeeffff adjust_general(value, amount): rrrreeeettttuuuurrrrnnnn value * amount ddddeeeeffff adjust_default(value): rrrreeeettttuuuurrrrnnnn adjust_general(value, 2.0) Python Functions
  • 69.
    Parameters that havedefaults must come after parameters that do not Python Functions
  • 70.
    Parameters that havedefaults must come after parameters that do not ddddeeeeffff triplet(left='venus', middle, right='mars'): rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) Python Functions
  • 71.
    Parameters that havedefaults must come after parameters that do not ddddeeeeffff triplet(left='venus', middle, right='mars'): rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) pppprrrriiiinnnntttt triplet('earth') venus earth mars OK so far... Python Functions
  • 72.
    Parameters that havedefaults must come after parameters that do not ddddeeeeffff triplet(left='venus', middle, right='mars'): rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) pppprrrriiiinnnntttt triplet('earth') venus earth mars pppprrrriiiinnnntttt triplet('pluto', 'earth') OK so far... ? Python Functions
  • 73.
    Parameters that havedefaults must come after parameters that do not ddddeeeeffff triplet(left='venus', middle, right='mars'): rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) pppprrrriiiinnnntttt triplet('earth') venus earth mars pppprrrriiiinnnntttt triplet('pluto', 'earth') OK so far... ? triplet('pluto', 'earth', 'mars') Python Functions
  • 74.
    Parameters that havedefaults must come after parameters that do not ddddeeeeffff triplet(left='venus', middle, right='mars'): rrrreeeettttuuuurrrrnnnn '%s %s %s' % (left, middle, right) pppprrrriiiinnnntttt triplet('earth') venus earth mars pppprrrriiiinnnntttt triplet('pluto', 'earth') OK so far... ? triplet('pluto', 'earth', 'mars') triplet('venus', 'pluto', 'earth') Python Functions
  • 75.
    "When should Iwrite a function?" Python Functions
  • 76.
    "When should Iwrite a function?" Human short term memory can hold 7± 2 items Python Functions
  • 77.
    "When should Iwrite a function?" Human short term memory can hold 7± 2 items If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss in their mind at once to understand a block of code, it's too long Python Functions
  • 78.
    "When should Iwrite a function?" Human short term memory can hold 7± 2 items If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss in their mind at once to understand a block of code, it's too long Break it into comprehensible pieces with functions Python Functions
  • 79.
    "When should Iwrite a function?" Human short term memory can hold 7± 2 items If someone has to keep more tthhaann aa ddoozzeenn tthhiinnggss in their mind at once to understand a block of code, it's too long Break it into comprehensible pieces with functions Even if each function is only called once Python Functions
  • 80.
    Example ffffoooorrrr xiiiinnnn range(1, GRID_WIDTH-1): ffffoooorrrr y iiiinnnn range(1, GRID_HEIGHT-1): iiiiffff (density[x-1][y] > density_threshold) or (density[x+1][y] > density_threshold): iiiiffff (flow[x][y-1] < flow_threshold) or (flow[x][y+1] < flow_threshold): temp = (density[x-1][y] + density[x+1][y]) / 2 iiiiffff abs(temp - density[x][y]) > update_threshold: density[x][y] = temp Python Functions
  • 81.
    Refactoring #1: gridinterior ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff (density[x-1][y] > density_threshold) or (density[x+1][y] > density_threshold): iiiiffff (flow[x][y-1] > flow_threshold) or (flow[x][y+1] > flow_threshold): temp = (density[x-1][y] + density[x+1][y]) / 2 iiiiffff abs(temp - density[x][y]) > update_threshold: density[x][y] = temp Python Functions
  • 82.
    Refactoring #2: testson X and Y axes ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff density_exceeds(density, x, y, density_threshold): iiiiffff flow_exceeds(flow, x, y, flow_threshold): temp = (density[x-1][y] + density[x+1][y]) / 2 iiiiffff abs(temp - density[x][y]) > tolerance: density[x][y] = temp Python Functions
  • 83.
    Refactoring #3: updaterule ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff density_exceeds(density, x, y, density_threshold): iiiiffff flow_exceeds(flow, x, y, flow_threshold): update_on_tolerance(density, x, y, tolerance) Python Functions
  • 84.
    Refactoring #3: updaterule ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff density_exceeds(density, x, y, density_threshold): iiiiffff flow_exceeds(flow, x, y, flow_threshold): update_on_tolerance(density, x, y, tolerance) Good programmers will write this first Python Functions
  • 85.
    Refactoring #3: updaterule ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff density_exceeds(density, x, y, density_threshold): iiiiffff flow_exceeds(flow, x, y, flow_threshold): update_on_tolerance(density, x, y, tolerance) Good programmers will write this first Then write the functions it implies Python Functions
  • 86.
    Refactoring #3: updaterule ffffoooorrrr x iiiinnnn grid_interior(GRID_WIDTH): ffffoooorrrr y iiiinnnn grid_interior(GRID_HEIGHT): iiiiffff density_exceeds(density, x, y, density_threshold): iiiiffff flow_exceeds(flow, x, y, flow_threshold): update_on_tolerance(density, x, y, tolerance) Good programmers will write this first Then write the functions it implies Then refactor any overlap Python Functions
  • 87.
    created by GregWilson October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.