Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Private" (implementation) class in Python

People also ask

What is a private class in Python?

Private methods are those methods that should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private methods that cannot be accessed except inside a class. However, to define a private method prefix the member name with double underscore “__”.

How do you make class data private in Python?

But there is a method in Python to define Private: Add “__” (double underscore ) in front of the variable and function name can hide them when accessing them from out of class. Python doesn't have real private methods, so one underline in the beginning of a method or attribute means you shouldn't access this method.

Is __ init __ a private method?

A notable private method in Python is the __init__() method, which is used as a class constructor for a class object. This method is called when an object of a class is instantiated, depending on the method's arguments.

Are Python classes Public or private?

All members in a Python class are public by default. Any member can be accessed from outside the class environment. You can access the Student class's attributes and also modify their values, as shown below.


Use a single underscore prefix:

class _Internal:
    ...

This is the official Python convention for 'internal' symbols; "from module import *" does not import underscore-prefixed objects.

Edit: Reference to the single underscore convention


In short:

  1. You cannot enforce privacy. There are no private classes/methods/functions in Python. At least, not strict privacy as in other languages, such as Java.

  2. You can only indicate/suggest privacy. This follows a convention. The python convention for marking a class/function/method as private is to preface it with an _ (underscore). For example, def _myfunc() or class _MyClass:. You can also create pseudo-privacy by prefacing the method with two underscores (eg: __foo). You cannot access the method directly, but you can still call it through a special prefix using the classname (eg: _classname__foo). So the best you can do is indicate/suggest privacy, not enforce it.

Python is like perl in this respect. To paraphrase a famous line about privacy from the Perl book, the philosophy is that you should stay out of the living room because you weren't invited, not because it is defended with a shotgun.

For more information:

  • Private variables Python Documentation
  • Why are Python’s ‘private’ methods not actually private? StackOverflow question 70528

Define __all__, a list of names that you want to be exported (see documentation).

__all__ = ['public_class'] # don't add here the 'implementation_class'

A pattern that I sometimes use is this:

Define a class:

class x(object):
    def doThis(self):
        ...
    def doThat(self):
        ...

Create an instance of the class, overwriting the class name:

x = x()

Define symbols that expose the functionality:

doThis = x.doThis
doThat = x.doThat

Delete the instance itself:

del x

Now you have a module that only exposes your public functions.