Functions as object python
Functions as object python
FUNCTIONS as OBJECTS
def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even and False otherwise
"""
return i%2 == 0
1
12/12/2024
WHAT IF THERE IS
NO return KEYWORD
def is_even( i ):
"""
Input: i, a positive int
Does not return anything
"""
i%2 == 0
def is_even( i ):
"""
Input: i, a positive int
Does not return anything
"""
i%2 == 0
return None
2
12/12/2024
add(1,2)
print(add(2,3))
mult(3,4)
print(mult(4,5))
3
12/12/2024
FUNCTION SCOPE
4
12/12/2024
UNDERSTANDING FUNCTION
CALLS
ENVIRONMENTS
Global environment
Where user interacts with Python interpreter
Where the program starts out
Invoking a function creates a new environment (frame/scope)
5
12/12/2024
VARIABLE SCOPE
xxy = 3
z = f( x )
VARIABLE SCOPE
after evaluating def
def f( x ):
Global scope
x = x + 1
function
Some
print('in f(x): x =', x) f object
code
return x
x = 3
z = f( x )
6
12/12/2024
VARIABLE SCOPE
after exec 1st assignment
def f( x ):
Global scope
x = x + 1
Some
print('in f(x): x =', x) f code
return x
3
x
x = 3
z = f( x )
VARIABLE SCOPE
after f invoked
x = 3
z = f( x )
7
12/12/2024
VARIABLE SCOPE
after f invoked
Global scope
def f( x ):
Some f scope
x = x + 1 f
code 3
print('in f(x): x =', x) x
return x 3
y
y = 3
z = f( y )
VARIABLE SCOPE
eval body of f in f’s scope
x = 3
z = f( x )
8
12/12/2024
VARIABLE SCOPE
during return
Global scope
def f( x ): f scope
x = x + 1 f Some
code x
print('in f(x): x =', x) 4
return x 3
x
returns 4
x = 3
z = f( x )
VARIABLE SCOPE
after exec 2nd assignment
def f( x ):
Global scope
x = x + 1
Some
print('in f(x): x =', x) f code
return x
3
x
x = 3
4
z = f( x )
z
9
12/12/2024
BIG IDEA
You need to know what
expression you are executing
to know the scope you are in.
10
12/12/2024
FUNCTIONS as
ARGUMENTS
11
12/12/2024
OBJECTS IN A PROGRAM
function
object with
my_func
some code
is_even
pi = 22/7 False
a
my_func = is_even
b True
a = is_even(3)
b = my_func(4)
12
12/12/2024
BIG IDEA
Everything in Python is
an object.
FUNCTION AS A PARAMETER
def calc(op, x, y):
return op(x,y)
def add(a,b):
return a+b
def div(a,b): if
b != 0:
return a/b
print("Denominator
was 0.")
print(calc(add, 2, 3))
13
12/12/2024
res
res
14
12/12/2024
res
Program Scope
def add(a,b): calc scope
return a+b calc function
Some add
object
code op
def div(a,b):
Some
function 2
if b != 0: add x
code
object
return a/b
print("Denom was 0.")
function
Some 3
div y
object
code
res = calc(add, 2, 3)
res
15
12/12/2024
res
res
16
12/12/2024
res
returns 5
Program Scope
def add(a,b): calc scope
return a+b calc function
Some add
object
code op
def div(a,b):
Some
function 2
if b != 0: add x
code
object
return a/b
print("Denom was 0.")
function
Some 3
div y
object
code
res = calc(add, 2, 3)
res
17
12/12/2024
res
returns 5
18
12/12/2024
def div(a,b):
if b != 0:
return a/b
print("Denom was 0.")
res = calc(div,2,0)
ANOTHER EXAMPLE:
FUNCTIONS AS PARAMS
def func_a():
print('inside func_a')
def func_b(y):
print('inside func_b')
return y
def func_c(f, z):
print('inside func_c')
return f(z)
print(func_a())
print(5 + func_b(2))
print(func_c(func_b, 3))
19
12/12/2024
FUNCTIONS AS PARAMETERS
func_a scope
Global scope
def func_a(): Some
print('inside func_a') func_a code
def func_b(y): Some
print('inside func_b') func_b code
return y Some
def func_c(f, z): code
func_c
print('inside func_c')
return f(z)
print(func_a())
print(5 + func_b(2))
print(func_c(func_b, 3))
FUNCTIONS AS PARAMETERS
20
12/12/2024
FUNCTIONS AS PARAMETERS
Global scope
def func_a():
Some
print('inside func_a') func_a code
def func_b(y): Some
print('inside func_b') func_b code
return y Some
def func_c(f, z): code
func_c
print('inside func_c')
return f(z)
print(func_a())
print(5 + func_b(2))
print(func_c(func_b, 3))
FUNCTIONS AS PARAMETERS
print(func_a())
print(5 + func_b(2))
print(func_c(func_b, 3))
21
12/12/2024
FUNCTIONS AS PARAMETERS
FUNCTIONS AS PARAMETERS
print(5 + func_b(2))
print(func_c(func_b, 3))
22
12/12/2024
FUNCTIONS AS PARAMETERS
Global scope
def func_a():
func_a Some
print('inside func_a') code
def func_b(y): Some
print('inside func_b') func_b code
return y Some
def func_c(f, z): func_c code
print('inside func_c') None
return f(z)
print(func_a()) 7
print(5 + func_b(2))
print(func_c(func_b, 3))
FUNCTIONS AS PARAMETERS
print(func_a()) 7
print(5 + func_b(2))
print(func_c(func_b, 3))
23
12/12/2024
FUNCTIONS AS PARAMETERS
print('inside None
returns 3 func_b scope
return f(z)
y 3
print(func_a()) 7
print(5 + func_b(2))
print(func_c(func_b, 3))
FUNCTIONS AS PARAMETERS
24
12/12/2024
49
SUMMARY
25