Private class methods in Python

yobbwqpd report abuse

I used to work with Java and there were private methods. Is there something like this in Python?

Answers

Olya_Olya report abuse

Python doesn’t support private methods by design. But there is a convention to use a single underscore to point that method is private. You shouldn’t use such methods outside of the class where they are defined. Sometimes programmers mistake name mangling for a tool to make a method private. It uses two underscores before the method name and looks like it hides method for outside the class. But it’s not hide it, only rename. Name mangling uses for class inheritance when we have methods with the same name. You shouldn’t use it for private methods, it’s a bad practice.

benNy024 report abuse

There is a detailed explanation for using underscores in this question https://imaginghub.com/forum/posts/1429-double-underscore-vs-single-underscore-before-python-method

Add Answer

Need support?

Just drop us an email to ... Show more