"Private" (implementation) class in Python

Question:

I am coding a small Python module composed of two parts:

  • some functions defining a public interface,
  • an implementation class used by the above functions, but which is not meaningful outside the module.

At first, I decided to “hide” this implementation class by defining it inside the function using it, but this hampers readability and cannot be used if multiple functions reuse the same class.

So, in addition to comments and docstrings, is there a mechanism to mark a class as “private” or “internal”? I am aware of the underscore mechanism, but as I understand it it only applies to variables, function and methods name.

Asked By: oparisy

||

Answers:

The convention is prepend “_” to internal classes, functions, and variables.

Answered By: Benjamin Peterson

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.

Reference to the single underscore convention.

Answered By: Ferdinand Beyer

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

__all__ = ['public_class'] # don't add here the 'implementation_class'
Answered By: UncleZeiv

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.

Answered By: theller

Use two underscores to prefix names of “private” identifiers. For classes in a module, use a single leading underscore and they will not be imported using “from module import *”.

class _MyInternalClass:
    def __my_private_method:
        pass

(There is no such thing as true “private” in Python. For example, Python just automatically mangles the names of class members with double underscores to be __clssname_mymember. So really, if you know the mangled name you can use the “private” entity anyway. See here. And of course you can choose to manually import “internal” classes if you wanted to).

Answered By: chroder

To address the issue of design conventions, and as chroder said, there’s really no such thing as "private" in Python. This may sound twisted for someone coming from C/C++ background (like me a while back), but eventually, you’ll probably realize following conventions is plenty enough.

Seeing something having an underscore in front should be a good enough hint not to use it directly. If you’re concerned with cluttering help(MyClass) output (which is what everyone looks at when searching on how to use a class), the underscored attributes/classes are not included there, so you’ll end up just having your "public" interface described.

Plus, having everything public has its own awesome perks, like for instance, you can unit test pretty much anything from outside (which you can’t really do with C/C++ private constructs).

Answered By: Dimitri Tcaciuc

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 (for example, __foo). You cannot access the method directly, but you can still call it through a special prefix using the classname (for example, _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:

Answered By: Karl Fast

I’m new to Python but as I understand it, Python isn’t like Java.
Here’s how it happens in Python:

class Student:

    __schoolName = 'XYZ School'      # private attribute
    def __nameprivamethod(self):     # private function 
         print('two underscore')

class Student:

    _schoolName = 'XYZ School'       # protected attribute

Don’t to check how to access the private and protected parts.

Answered By: MAHDI NOUIRA

In fact you can achieve something similar to private members by taking advantage of scoping. We can create a module-level class that creates new locally-scoped variables during creation of the class, then use those variables elsewhere in that class.

class Foo:
    def __new__(cls: "type[Foo]", i: int, o: object) -> "Foo":
        _some_private_int: int = i
        _some_private_obj: object = o
        foo = super().__new__(cls)
        def show_vars() -> None:
            print(_some_private_int)
            print(_some_private_obj)
        foo.show_vars = show_vars
        return foo

    def show_vars(self: "Foo") -> None:
        pass

We can then do, e.g.

foo = Foo(10, {"a":1})
foo.show_vars()
# 10
# {'a': 1}

Alternatively, here’s a poor example that creates a class in a module that has access to variables scoped to the function in which the class is created. Do note that this state is shared between all instances (so be wary of this specific example). I’m sure there’s a way to avoid this, but I’ll leave that as an exercise for someone else.

def _foo_create():
    _some_private_int: int
    _some_private_obj: object
    class Foo:
        def __init__(self, i: int, o: object) -> None:
            nonlocal _some_private_int
            nonlocal _some_private_obj
            _some_private_int = i
            _some_private_obj = o

        def show_vars(self):
            print(_some_private_int)
            print(_some_private_obj)

    import sys
    sys.modules[__name__].Foo = Foo
_foo_create()

As far as I am aware, there is not a way to gain access to these locally-scoped variables, though I’d be interested to know otherwise, if it is possible.

Answered By: aholmes
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.