4 The object of conversation

 

This chapter covers

  • Object-oriented APIs
  • Objects with classes
  • Inheritance
  • Polymorphism
  • Composition

When having a conversation, particularly one with any complexity, it’s helpful if everyone in the conversation has the same context. It would be difficult to have conversations if every time someone began a new sentence they had to present the full context of the conversation.

From the standpoint of software functions, the context is the current state of the information the functions are working with. In the previous chapter, we talked about creating function signatures where the data state is passed around to the function calls in consistent ways.

Utilizing function signatures is a useful and powerful way to conduct conversations between functions that work on stateful data. It becomes a little more complicated if the same functions are being passed multiple, distinct stateful data contexts. The data and the functions that work on that data are separate from each other, and it’s up to the developer to keep them organized and connected. Python provides another layer of abstraction to reduce complexity by using the object-oriented programming model.

4.1 Object-oriented programming (OOP)

The ability to place functions into modules provides many opportunities for structuring an API. The type and order of the parameters passed to the functions that make up an API offer possibilities to make your API more discoverable and useful.

4.1.1 Class definition

4.1.2 Drawing with class

4.1.3 Inheritance

4.1.4 Polymorphism

4.1.5 Composition

4.2 Closing thoughts

Summary