Member-only story
How to Define Nonpublic Methods in a Python Class
Idiomatic ways to use Python as OOP
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…