How to implement private methods in Python classes?

How can I implement python private methods in a class? I want to have a function that will only be used inside the methods of this class and not called outside of them. In C++, I would declare the method in the private section of the class. What is the best way to implement such a function in Python?

I am considering using a static decorator for this case. Can I use a function without any decorators and without the self keyword?

So, I’ve been working with Python for a while, and here’s the thing: Python doesn’t have true private methods or attributes like in C++. But you can simulate python private methods through something called name mangling. It’s a neat trick that helps prevent external access. When you use two underscores before a method or variable, it gets “mangled,” which makes it harder (but not impossible) to access outside the class.

Here’s how it works: If you have a method like __private(), Python changes its name to something like _A__private internally. This makes it less likely to clash with other methods, especially in subclasses. Check this out:

class A:
    def __private(self):
        pass

A._A__private()  # This works, but it's not recommended!

In this case, the method __private is now _A__private. You can technically still access it, but it’s sort of like a gentle “don’t touch” sign. If you really want to call this method from outside, you’d do it like this:

class A:
    @staticmethod
    def __private():
        print('hello')

A._A__private()  # It works, but keep in mind it's a hack, not true privacy!

So, while python private methods aren’t locked down, this is how you can “fake” privacy in Python.

Right, continuing from @panchal_archanaa’s point, you can also consider using a single underscore (_private) to indicate that a method or attribute is intended to be private within the class, though it’s not enforced by Python. This is more of a convention—it’s like saying, “Hey, this is for internal use only,” but you can still access it from outside the class if you really need to.

It’s not really about restricting access; it’s more about signaling to other developers that you shouldn’t be messing with these methods unless you know what you’re doing. It’s kind of a gentle reminder, like saying “please don’t” rather than “you can’t.”

Here’s an example:

class A:
    def _private(self):
        pass

In this case, _private is still accessible from outside the class, but by convention, it’s meant to stay within the class. It’s up to the programmer to respect that convention when using python private methods.

Building on what @shilpa.chandel mentioned, if you need to work with class-level data but want a method that’s somewhat private, you could use a class method. Class methods aren’t strictly private, but they give you a way to interact with the class itself (via cls). They’re great for when you need access to class-level data, but you still want to maintain some level of control over how things are used.

Even though this method isn’t technically a private one, it can serve as a controlled way of dealing with the internal workings of the class, almost like a python private method for class-level operations.

Here’s an example:

class A:
    @classmethod
    def _private(cls):
        pass

In this case, _private is designed to work with the class, not instances. It’s still following the convention of being pseudo-private, but it gives you flexibility when you need to access class data internally.