Untitled Document
First Class Functions
1. Introduction Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Introduction 
       
       # This is advanced topir of Python useful for understanding Decorators . 
       # Rather than looking into the Therotical Definitions . Lets Run some sime codes to understand the concept 
       # IN below example we will create a simple function and then we will assign that function to a variable 
       # Then we will execute that varibale as function . 
       
       
2. Simple Example of First Class Function Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Simple Example of First Class Function

def square(x):
	return x * x
f = square # assinged sqaure function to variable f 

print(square)
print(f(5)) # executed function using variable        

       
3. Higher Order Function Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Higher Order Function 
       
# In this example we will pass a function as an argumet to a function and also we will return a result as function from
# a function - Such type of Functions are called higher order functions 

# EX  - A 
# Here we will pass a function as an argument to another function .
# We have two functions square and cube which we will pass to my_map function 
# The result will be stored in a list .
# The my_map function takes two arguments 1. function to be executed 2. List of elements

def square(x):
	return x * x

def cube(x):
	return x * x * x

def my_map(func, arg_list):
	result = []
	for i in arg_list:
		result.append(func(i))
	return result

cubes = my_map(cube, [1, 2, 3, 4, 5])
print(cubes)

# For squares 

squares = my_map(square,[1, 2, 3, 4, 5])
print(squares)

# EX - B

# The below code shows the simple example of returning a function as a result from a function 

def logger(msg):
	def log_message():
		print('Log:', msg)

	return log_message

log_hi = logger('Hi !')
log_hi()

# EX - C
# In this example we are passing value to inner function 
# Just run the example and check the output

def html_tag(tag):

	def wrap_text(msg):
		print('<{0}>{1}'.format(tag, msg))

	return wrap_text

print_h1 = html_tag('h1')
print_h1('Test Headline !')
print_h1('Another Headline!')

print_p = html_tag('p')
print_p('Test Paragraph!')

            
Untitled Document