Python 3.4 - First-Class Functions

 You can assign functions to variables. And you can have functions within functions.

def square(x): # this function will return the value of x times itself    return x * x

f = square(5) # this is the variable and the value of x is determined
print(square) # just printing out the position of the functionprint(square(3)) # this is a stand-alone print statement calling on the function, not creating a variableprint(f) # this prints the results of the function which will take 5 and multiply it by itself
h = square # this makes the variable h the equivalent of the function square.
print(h) # just printing out the position of the function
print(h(4)) # now h is being treated as the function square



print('--------------------------------------------')







def my_map(func, arg_list): # this sets up the function to return a result,    # which will be using the function on the array    result = []
    for i in arg_list:
        result.append(func(i))
    return result

squares = my_map(square, [1, 2, 3, 4]) # this uses the function and previous function to return the result

print(squares) # this prints the results of the functions
def cube(x): #again here is a function to find the cube of a number.    return x * x * x


def my_cubes(func, arg_list): # this sets up the function, using the other function    # to return a result    result = []
    for i in arg_list:
        result.append(func(i))
    return result

cubes = my_cubes(cube, [2,4,5,7]) # this again is the variable calling on the functions
print(cubes) # this returns the results

cubes2 = my_map(cube, [2,3,55,66]) # this in interesting. this uses the function cube.                                   # so you can use any function in the my_map function.                                   # Because as you can see why do it twice.
print(cubes2)


print('--------------------------------------------')

def logger(msg): # this function uses the function within it and executes a print function

    def log_message():
        print('Log:', msg) # this is the print function
    return log_message

log_hi = logger('Hi!') # this sets the variable log_hi as the function loggerlog_hi()               # this runs the function
print(log_hi()) # this also runs the function,                # but also returns 'none' since nothing is in the brackets.

print('-------------------------------------------')

def html_tag(tag):  #this function executes the code below. and takes a tag variable
    def wrap_text(msg): # this function prints the tag then message then tag again        print('<{0}>{1}</{0}>'.format(tag, msg))

    return wrap_text

print_h1 = html_tag('h1') # this creates a function with the tags h1print_h1('test headline') # this uses the function print_h1 and includes the messageprint_h1('another headline') # this does the same, with a different message
print_p = html_tag('p') # this does it again with a different set tagprint_p('test paragraph') # and a different message
print('-------------------------------------------')

 /Users/mattfassnacht/PycharmProjects/untitled10/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled10/math.py
<function square at 0x101860ea0>
9
25
<function square at 0x101860ea0>
16
--------------------------------------------
[1, 4, 9, 16]
[8, 64, 125, 343]
[8, 27, 166375, 287496]
--------------------------------------------
Log: Hi!
Log: Hi!
None
-------------------------------------------
<h1>test headline</h1>
<h1>another headline</h1>
<p>test paragraph</p>
-------------------------------------------

Process finished with exit code 0


https://www.youtube.com/watch?v=kr0mpwqttM0

Comments

Popular posts from this blog

Pyhton 3.4 - Continue

Conversations Ongoing: Valuing Modernization in Early European History

Conversations Ongoing: A Bunch of Amateurs