Composition over inheritance ? What about pythonic code

Siddharth Goyal (~sid22)


0

Votes

Description:

“Program to an interface, not an implementation.” - Gang of Four book

"Favor composition over inheritance!" is a tale as old as programming itself now. There are dedicated thoughts professing this paradigm for object oriented programming. However, following this religiously in python and not using any type of sub-classing we end up with non-pythonic code that is often to verbose.

A crucial weakness of inheritance as a design strategy is that a class often needs to be specialized along several different design axes at once, leading a proliferation of classes and an explosion of subclasses to support every combination. However, inheritance has a lot of general advantages especially when starting new projects.

Python also provides novel ways like multiple class inheritance, dynamic class building, mixins. All of the come with their own advantages and disadvantages. Multiple class inheritance specifically looks like a great way to leverage functionalities across multiple classes but has it's shady underbelly like MRO, duplicate overrides etc.

In this talk we will go over how to write clutter free, maintainable pythonic code and look at all the approaches and their advantages and dis-advantages without any bias. By the end of this talk you should have a better idea of the multiple approaches you can take to subclassing to leverage common methods, improve readability and composition patterns to leverage the flexibility of non-rigid code.

Outline of the talk:

  1. Introduction
  2. General overview about OOP
  3. What is inheritance, what is composition with examples ?
  4. Why composition over inheritance ?
  5. Pythonic issues with composition over inheritance with examples ?
  6. Approaches to python OOP design patterns that are pythonic ( adapter, bridge and decorator patterns ) with examples.
  7. Multiple class inheritance, mixinis advantages and disadvantages with examples.
  8. Closing remarks with notes on type checking and how it improves all above points.

Prerequisites:

No pre-requisites

Content URLs:

  • https://en.wikipedia.org/wiki/Composition_over_inheritance
  • https://docs.python.org/3/library/typing.html
  • PPT

Speaker Info:

Siddharth is a software engineer at Rubrik where he has worked on multiple data intensive high performant, compute optimized services and packages. He has worked in multiple open source projects and is an active member of OWASP project. He has previous experience working at Innovaccer and some of his focus areas are cloud computing, few shot learning, reinforcement learning, mathematical proofing, compute optimized data intensive applications etc.

Speaker Links:

LinkedIn

Github

  • https://www.youtube.com/watch?v=t4P8tr6m0ns
  • https://owasp.blogspot.com/2017/07/owasp-code-sprint-2017-student.html

Section: Core Python
Type: Talks
Target Audience: Beginner
Last Updated: