Day 4 - Nothing But the Func

Today’s mission was completing both sections of the Udemy class on functions. I’ve encountered functions in python before, but to my amazement I learned two wickedly awesome and mindblowing concepts:

The concept of anonymous functions is boss . One thing that confused me though, and I’ve yet to completely understand. Check this out:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import functools

def sum(x,y):
    return x + y

numbers = list(range(1,11))
sum = functools.reduce(sum, numbers)
print sum

In the section about higher order functions (aka functions that act on other functions), they introduced the reduce function via this example. Now the concept is pretty cool- using this higher order function, you’re able to assign a function to the list data structure, apply each of the list indicies as arguments in the sum function, and return the sum of all the numbers in the range. What I don’t quite understand is how it works with a function that has more than one argument. Does the reduce function somehow know how to properly iterate through the list? How is it keeping up with the list indicies that have been passed to the sum function? Not exactly sure yet, but functions that can ‘act’ on other functions is awesome. I think I grew pythonic legs today as opposed to any other day so far during my challenge.

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin