Professional Documents
Culture Documents
Lecture 6
Lecture 6
Russel L. Villacarlos
College of Engineering and Instructor / Professor
Information Technology
Function Objects
def greet(greeting, person):
print(greeting, person)
• Functions (and methods) in
def make_repeating(step, fn):
Python are objects def repeating_fn(*args):
for i in range(step):
fn(*args)
• The def function_name() return repeating_fn
statement creates a
greeting_func = greet
function object and assign
it to a variable named #Outputs <class 'function'>
print(type(greet))
function_name print(type(greeting_func))
greeting_func = greet
a = 1
• This is called the local def outer():
b = 2
namespace def inner():
c = 3
print(a,b,c)
College of Engineering and
Information Technology
LEGB Rule
a = 1
def outer():
b = 2
def inner():
c = 3
print(a,b,c)
College of Engineering and
Information Technology
LEGB Rule
Local
• This is called the global
namespace a = 1
def outer():
b = 2
def inner():
• The global namespace c = 3
includes all imported objects print(a,b,c)
College of Engineering and
from Technology
Information other modules
LEGB Rule
• The result of the expression becomes the return value of the lambda
function
• Ex: lambda a,b: a + b
College• ofEx: lambda person:
Engineering and person['name'].upper()
Information Technology
Lambda