This talk was presented at PyBay2019 - 4th annual Bay Area Regional Python conference. See pybay.com for more details about PyBay and click SHOW MORE for more information about this talk.
Description The strengths and weaknesses of Python lend themselves to a different style of object oriented programming. By accepting several constraints on how we design and implement classes, we make our code more robust, more testable, and easier to adapt to changing circumstances.
Abstract Detailed outline Introduction (2m) Python and object oriented programming (1m) Why OO design principles? (1m) Interfaces (4m) Delve into the best practices of explicit (sometimes called "goosely typed") interfaces in Python, and showing how to use those with the zope.interface system.
Why declare interfaces? Declaring interfaces with zope.interface. Implementing and providing interfaces with zope.interface. Testing for interface provision. Boring constructors (4m) Newcomers to Python often put non-trivial code in the constructors. This is often a bad idea. This section will cover why, and show where is the right place to put non-trivial initialization logic.
Boring vs. interesting constructors. Why boring constructors. Moving interesting stuff to class methods. Using attrs. Boring objects (4m) "Objects are data and behavior" -- following this principle in a naive way leads to putting a lot of code inside objects, creating unwieldy objects and unwieldy interfaces. This section will discuss why that is problematic, and show-case some alternatives.
Why make objects boring? (1m) What's singledispatch (1m) Example using functools.singledispatch (2m) Immutability (4m) Shared mutable state is the root of all evil -- and in Python, a lot of things are shared! One way to avoid evil is to reduce mutability. This section will discuss how to avoid mutability while keeping reasonable performance and ergonomic APIs.
Immutability as bug-avoidance (1m) Immutability as simpler interfaces (1m) Frozen attrs Efficiently immutable data structures Avoiding Private Methods (4m) Private methods are often under-documented crutches. This section will discuss the kind of problems they can cause, and describe good alternatives as well as an almost-mechanical refactoring to avoid private methods.
What's wrong with private methods? (1m) Using private attributes (1m) Refactoring private methods away (2m) Composition (4m) Traditional object-orientation stressed inheritance. However, for a long time, it has been known that inheritance is problematic -- even the GoF "Design Patterns" covers how to avoid it. This section will cover the problems it causes, and good alternatives.
Inheritance-as-API (1m) Problems with inheritance-as-API (1m) What is composition? (1m) Composition-as-API (1m) Summary (3m) Bringing this all to a close, the summary will show the common thread in all of these -- that keepings objects simple and predictable ("boring") leads to code that is easier to maintain.
Python affords near-infinite freedom (1m) Choices have consequences (1m) Know the rules to break the rules (1m)
Original slides: https://t.ly/6g1rZ
About the speaker Moshe has been in the open source community since 1995 and in the Python community since 1999. He has been a core contributor to Python, and is a core contributor, and an early member, of the Twisted project.
Moshe has given talks at the International Python Conference, PyCon, PyBay, PyTexas and several Python and Docker meetups.
Sponsor Acknowledgement This and other PyBay2019 videos are via the help of our media partner AlphaVoice (https://www.alphavoice.io/)!
#pybay #pybay2019 #python #python3