0% found this document useful (0 votes)
17 views1 page

Decorator Python

The document demonstrates how to assign a function to a variable, showing that 'say_hi' can be used to call the 'greet' function. It also illustrates passing a function as an argument to another function and returning a function from within another function, specifically creating a multiplier. The examples provided include greeting names and multiplying numbers.

Uploaded by

samswag2898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Decorator Python

The document demonstrates how to assign a function to a variable, showing that 'say_hi' can be used to call the 'greet' function. It also illustrates passing a function as an argument to another function and returning a function from within another function, specifically creating a multiplier. The examples provided include greeting names and multiplying numbers.

Uploaded by

samswag2898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Assigning a function to a variable

def greet(n):

return f"Hello, {n}!"

say_hi = greet # Assign the greet function to say_hi

print(say_hi("Alice")) # Output: Hello, Alice!

# Passing a function as an argument

def apply(f, v):

return f(v)

res = apply(say_hi, "Bob")

print(res) # Output: Hello, Bob!

# Returning a function from another function

def make_mult(f):

def mult(x):

return x * f

return mult

dbl = make_mult(2)

print(dbl(5)) # Output: 10

You might also like