Making class methods not accessible

Hello, everyone.
Greetings of the new year!

I was working with classes and I just realized that there is a piece of code which is being reused under several methods of this class. So, I want to take this reused piece of code and make it a function of it’s own. However, I don’t want this function to act like it is a method of this class, meaning it shouldn’t be accessible via the class object or it’s instances. Is there any way of writing a function like this? I’ll try to explain what I’m doing with code below:

class someName:

  def class_method_1(self):
     self.reused_function()
    # some other code

  def class_method_2(self):
     self.reused_function()

  def reused_function(self):
     # some code

So, is there a way to make this reused function not available to class object or any of it’s instances and just be used for internal purposes of the class?

Thank you for taking the time to read this question.

There aren’t any proper private methods in Python but you can use name mangling so it’s a little clearer that it’s not supposed to be public and keeps it a little safer when subclassing-

3 Likes

Nice. I read through the documentation. This is exactly what I was looking for.

Thank you, @tgrtim.

1 Like