Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decorators 101: A Gentle Introduction to Functional Programming - Jillian Munson

Decorators 101: A Gentle Introduction to Functional Programming - Jillian Munson

"Functions are first-class objects in Python." This talk will discuss what we mean by first-class objects and how we can use this language feature to build more efficient, less redundant code with decorators without fear.

PyGotham 2014

August 17, 2014
Tweet

More Decks by PyGotham 2014

Other Decks in Programming

Transcript

  1. Programming with Functions Fun fact about functions in Python: source:

    https://docs.python.org/2.7/reference/compound_stmts.html#function- definitions
  2. Programming with Functions Fun fact about functions in Python: source:

    https://docs.python.org/2.7/reference/compound_stmts.html#function- definitions
  3. Programming with Functions Functions are first-class objects in Python, so

    everything below applies to functions! Tl;dl - In Python, we can pass functions to other functions as a parameter.
  4. Decorators What have we done?: 1. Created a function (‘decorated’)

    2. Defined a decorator (‘decorator’) 3. Write the decorator so that it returns a function definition (have ‘decorator’ return a locally defined function ‘inner_decorator’) 4. Tell the interpreter to pass a function to a decorator (‘@decorator’) and replace the original function with the function returned from our decorator.
  5. Decorators What if we wanted to “decorate” a function that

    takes parameters and do something with those parameters?
  6. Decorators Parameters & Decorators: What if we want our decorator

    to work on functions with a variable number of parameters?
  7. Decorators Things we can now do with decorators: - Add

    some behavior that executes before and after our original function - Grab the parameters being passed to the decorated function and do something with them - Apply the same decorator to functions with different numbers of arguments
  8. Decorators They can do things like: • Function profiling •

    Validate input to a function before execution • Format the output of a function • More!
  9. Decorators Where do we go from here? Classes as decorators?

    Decorators that decorate other decorators? (we need to go deeper) Look at some examples of large Python programs and learn how they implement functionality with decorators (i.e. Flask)
  10. Decorators Where do we go from here? Programming with Functions

    Other functional programming features in Python - Iterators, generators - Closures - List comprehensions Functional programming concepts - Pure functions - Lazy evaluation
  11. Decorators Final Thoughts Programming with Functions - Meta Lesson: We

    need to be more conscious about potential user holes when writing documentation.
  12. Decorators References Programming with Functions - this: http://bit.ly/1v4zrrA - wikipedia.org

    - python.org - docs.python.org/2/howto/functional.html - Flask: http://flask.pocoo.org/docs/api/ - Colton Meyer’s talk on decorators: http://pyvideo.org/video/2574/decorators-a- powerful-weapon-in-your-python - Graham Dumpleton on advanced methods using decorators: http://pyvideo.org/video/2617/advanced-methods-for-creat