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.

79ecef8e99fbedb7bca755c7ec1926f1?s=128

PyGotham 2014

August 17, 2014
Tweet

Transcript

  1. Decorators 101 (A Gentle Introduction to Functional Programming) Pygotham 2014

    Jillian Munson
  2. None
  3. jillian.munson@gmail.com github.com/jaemu @maykaynvwd

  4. None
  5. Decorators?

  6. Decorators? - are you saying my code is ugly?

  7. Decorators? - are you saying my code is ugly? (probably)

  8. Decorators:

  9. Decorators:

  10. Decorators:

  11. Decorators:

  12. Decorators Programming with Functions

  13. Programming with Functions This is what a function looks like:

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

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

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

  17. Programming with Functions Functions are first-class objects in Python -

    so what does that even mean?
  18. Programming with Functions Functions are first-class objects in Python, so

    everything below applies to functions!
  19. 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.
  20. Decorators Programming with Functions

  21. Decorators This is what a decorator looks like:

  22. Decorators This is what a decorator looks like: function

  23. Decorators This is what a decorator looks like: function decorator

  24. Decorators This is what a decorator looks like: function decorator

  25. Decorators

  26. Decorators

  27. 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.
  28. Decorators What if we wanted to “decorate” a function that

    takes parameters and do something with those parameters?
  29. Decorators function parameter decorator num_copy? Parameters & Decorators:

  30. Decorators function parameter decorator num_copy? Parameters & Decorators:

  31. Decorators Parameters & Decorators: What if we want our decorator

    to work on functions with a variable number of parameters?
  32. Decorators Parameters & Decorators:

  33. Decorators Parameters & Decorators: - same list of arguments

  34. Decorators Parameters & Decorators: Different number of parameters

  35. Decorators Parameters & Decorators: Different number of parameters

  36. 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
  37. Decorators They can do things like: • Function profiling •

    Validate input to a function before execution • Format the output of a function • More!
  38. 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)
  39. 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
  40. Decorators Final Thoughts Programming with Functions - Meta Lesson: We

    need to be more conscious about potential user holes when writing documentation.
  41. 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