What is a private method in Python?

Experience Level: Junior
Tags: Python

Answer

Private methods signal to readers of the code that the methods are/should be used only within the class. So if you want to rename the private method or change its signature, you know that it should be called only from the same class. Thanks to this you know that you won't break calls from other classes, because there should be none.

The word "should" is used intentionally because in Python it is possible to call a private method from outside. However, it is highly recommended not to do so, because the private method communicates: "This method was designed by its author as a method that the others shouldn't be calling.". So if you decide to call it and the author renames it in the future and your code breaks, it is your fault because you ignored a wish of the original author for you not to use the method.

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself