liveBook · Manning

Object-oriented programming

As explained in Python Workout: 50 ten-minute exercises

Object-oriented programming is a programming paradigm that organizes software around objects defined by classes containing state and behavior.

Object-oriented programming organizes data and the functions that operate on it together. Objects belong to classes, and their callable functions are methods. This replaces separate procedural lists of nouns and verbs with domain-specific types that make relationships clearer and support higher-level reasoning. section 9.0

Representing a set of scores

A procedural solution passes a list to an average function. An object-oriented solution defines ScoreList, stores the scores in an instance, and exposes average() as the operation belonging to that data. The calculation stays the same, but the caller works with a more explicit domain type. section 9.0

class ScoreList():
   def __init__(self, scores):
       self.scores = scores

   def average(self):
       return sum(self.scores) / len(self.scores)

scores = ScoreList([85, 95, 98, 87, 80, 92])
print(f'The final score is {scores.average()}.')

Shows a Python class with initialization, stored state, an instance method, and a caller using the resulting object. section 9.0

Useful object-oriented practice includes:

  • Extend a Zoo class so animals can be filtered by multiple colors, transfer between two zoo instances, or queried by color and legs. section 9.40
  • Use Python’s class features deliberately: __init__ initializes instances, __repr__ supplies printed representations, super() invokes parent-class methods, and @dataclass simplifies class definitions. section 9.0

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines classes, objects, methods, and selected Python features through a score example and Zoo exercises.
Examples
Uses procedural and ScoreList grade calculations plus Zoo filtering, transfer, and keyword-query exercises.
What you can do after
Build Python classes that combine stored data with methods and extend them through practical object-oriented exercises.
How it connects
Connects classes, objects, methods, procedural programming, abstraction, inheritance, and dataclasses.

Also explained in

Compare how 9 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.