TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

How to Define Nonpublic Methods in a Python Class

Yong Cui
6 min readJul 28, 2022

--

Photo by Nina Mercado on Unsplash

At its core, Python is an object-oriented programming (OOP) language, which builds its features around objects. A string is an object, an instance of a custom class is an object, and so is a class, module, and even package. One long-term principle in OOP coding is encapsulation — the design pattern where you expose the functionalities that the users need while hiding the implementation details and those that the users don’t need to have access to.

For any project, we define a bunch of custom classes as the basis for modeling various kinds of data. Thus, a good project means you define well-structured custom classes. When we work with these classes, we should apply encapsulation techniques. Although I don’t think we should treat encapsulation as dogma, you can consider two particular encapsulation features to increase the maintainability of your project — protected and private methods, which are intended to be used internally. This article is about showing you how to do so.

Introduction

As some of you may know, in some OOP languages, we have reserved keywords for encapsulation, such as protected, private, and public, which define how attributes (or methods) are accessible outside the class. However, Python doesn’t have…

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Yong Cui
Yong Cui

Written by Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Author of Python How-to by Manning (https://www.manning.com/books/python-how-to).

Responses (1)