''' Demonstrate functions can be pass as arguments ''' import functions_are_first_class values = [ 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 ] # The analyze function takes in two parameters (a list of values and a function to call on # that list). # In a1, we pass in the values list on line 7 and the len function identifier (len) # as arguments to the function analyze to call len(values) in the function analyze. print('id(len)=', id(len)) # The id function will tell you where in memory the thing in parentheses is. # We have used id() on variables but keep in mind that len can be used as a variable and is stored # in memory somewhere so we can use it. Functions also take up memory since functions # are identified using the names of the functions! # def function_name( parameters ): # function_name as a variable is stored in memory a1 = functions_are_first_class.analyze( values, len ) print( "len(", values, "):", a1 ) print() # In a2, we pass in the values list on line 7 and the max function identifier (max) # as arguments to the function analyze to call max(values) in the function analyze. a2 = functions_are_first_class.analyze( values, max ) print( "max(", values, "):", a2 ) print() # In a3, we pass in the values list on line 7 and the min function identifier (min) # as arguments to the function analyze to call min(values) in the function analyze. a3 = functions_are_first_class.analyze( values, min ) print( "max(", values, "):", a3 ) print() # In a4, we pass in the values list on line 7 and the sum function identifier (sum) # as arguments to the function analyze to call sum(values) in the function analyze. a4 = functions_are_first_class.analyze( values, sum ) print( "sum(", values, "):", a4 ) print()