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

Describing Descriptors

Describing Descriptors

Oftentimes beginner programmers go through traditional features when learning a language. For Python beginners this might involve variables, control structures like if-statements, while and for loops, dictionaries, and finally classes. However, if we read the Python documentation we find that another feature that can be used in Python is that of the descriptor protocol. Descriptors allow the programmer to override the storing and retrieving of different class instance variables such that special behaviours can be followed. For example, we might want some variable to follow some special validation. We could do this using __setattr__ on the containing class but perhaps we want to reuse the validation in another class, or we want other validations for other variables and we don’t want __setattr__ to become a huge if/elif/else block. In this talk, I will walk attendees through what a descriptor is, what use cases they can use them in, how to implement a descriptor, and common descriptors in the Python ecosystem that users may or may not have identified as descriptors (often just referred to as magic).

Matthew Egan

August 25, 2018
Tweet

Other Decks in Technology

Transcript

  1. • Matthew Egan • DiviPay • Sydneysider • mattjegan •

    NullMatthew • https://mattjegan.com Any opinions conveyed in this talk are my own and not my employers 2
  2. Thanks • Adam Jacquier-Parr - www.github.com/aljp • Christopher Di Bella

    - www.cjdb.com.au • Daniel Kniaz & Russell Martin - www.divipay.com 3
  3. Who is this for? • Everyone who is comfortable with

    classes in Python • I won’t be covering meta-classes in this talk 4
  4. Why Descriptors? • Descriptors are a feature of Python that

    is often overlooked • Used primarily by library developers rather than application developers 5
  5. PSA: Use Python 3 • This talk uses Python 3.6+

    • Python 2 is now at end of life and won’t be maintained after 2020 • Long live Python 3! 6
  6. What? • A problem • Some solutions • A better

    solution (hint: it’s descriptors) 7
  7. __setattr__ • Is called whenever we try to set an

    instance variable • Takes the calling object, the name of the attribute being set, and the new value 11
  8. 13

  9. __setattr__ • This does the job • Locked to this

    class • Gets out of hand with many properties 14
  10. 16

  11. @property and @name.setter • This does the job • Locked

    to this class • Gets out of hand with many properties 17
  12. The Descriptor Protocol • Python exposes the descriptor protocol as

    a way to customize the storing and retrieval of instance attributes • Self-contained • Reusable 20
  13. The Descriptor Protocol • __get__ is called when accessing the

    attribute • __set__ is called when storing the attribute • __delete__ is called when the attribute is being deleted • __set_name__ is called once when the class object is created (Python 3.6+, PEP487) 21
  14. 34

  15. Use Cases • Replicating customisation for multiple attrs without caring

    about name • Django generic foreign keys • Validation • Better error messages 35