liveBook · Manning

Object-oriented programming

As explained in Software Engineering for Data Scientists

Object-oriented programming is a programming paradigm that organizes software around objects containing state and behavior, commonly using classes, encapsulation, inheritance, and polymorphism.

Object-oriented programming (OOP) organizes code around classes and objects. A class groups functions and data; an object is an instance of that class. Methods are functions defined in classes, while instance variables are attributes attached to a particular object. A car offers a simple analogy: make, model, and color are attributes, while driving and parking are functions. section 4.0 section 4.4

For data science work, OOP helps turn convoluted notebook code into a more structured, flexible, and portable codebase. That can make feature-engineering and model-input processes easier to integrate with other code, support collaboration, and prepare code for production. It is one software engineering practice alongside source control, scaling, exception handling, and automated testing. section 4.0 section 1.5 section 1.0

Think in terms of these building blocks:

  • A class is a collection of functions and attributes (data). section 4.0
  • An object is a specific instance of a class; the integer 10, for example, is an instance of the integer class. section 4.4
  • Instance variables hold values for one object and may differ between objects created from the same class. section 4.4
  • Encapsulation means objects are self-contained and can control access to their functions or attributes. section 4.0

A random forest as an object

Python libraries already use OOP extensively. Importing RandomForestClassifier provides a class; creating forest_model makes an object with values such as n_estimators, max_depth, max_features, and random_state. Its methods can train the model, and dot notation reads an attached attribute such as forest_model.max_features. section 4.1.1

Listing 4.2 This code creates a RandomForestClassifier object and specifies several key parameters, including the number of trees (n_estimators = 100) and max_depth.
from sklearn.ensemble import RandomForestClassifier #1

forest_model = RandomForestClassifier( #2
    n_estimators = 100, #2
    max_depth = 4, #2
    max_features = "sqrt", #2
    random_state = 0) #2

This listing shows an object being created from RandomForestClassifier while supplying its initial attribute values. section 4.1.1

Because Python is full of objects, OOP is often already present in everyday work with integers, pandas, and scikit-learn. The practical next step is to recognize classes, objects, methods, and instance variables in library code, then use the same structure when building code that must remain understandable and reusable. section 4.1.1 section 4.0

How Software Engineering for Data Scientists covers it

How deep it goes
Defines classes, objects, methods, instance variables, encapsulation, and dot notation through a scikit-learn walkthrough.
Examples
Uses a car, an integer variable, and a scikit-learn RandomForestClassifier with configurable parameters.
What it assumes
Assumes basic Python use while introducing OOP for data scientists with limited prior exposure.
What you can do after
Apply it via scikit-learn by creating model objects, setting parameters, invoking methods, and reading attributes with dot notation.
How it connects
Connects OOP to source control, scaling, exception handling, automated testing, data pipelines, and machine learning pipelines.

Also explained in

Compare how 13 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.