Pure Function
A pure function does not modify any of the objects it receives as arguments. It only returns a new value.
Example (from 16.2):
class Time: pass def add_time(t1, t2): sum = Time() sum.hour = t1.hour + t2.hour sum.minute = t1.minute + t2.minute sum.second = t1.second + t2.second return sum
Usage:
start = Time() start.hour = 9 start.minute = 45 start.second = 0 duration = Time() duration.hour = 1 duration.minute = 35 duration.second = 0 done = add_time(start, duration)
This function does not alter start
or duration
. It returns a new Time
object done
.
Modifier Function
A modifier function changes the object it receives as an argument.
Example (from 16.3):
def increment(time, seconds): time.second += seconds if time.second >= 60: time.second -= 60 time.minute += 1 if time.minute >= 60: time.minute -= 60 time.hour += 1
Usage:
increment(start, 75)
This function modifies the existing start
object by incrementing its time.