When did the convention of leading underscores for private variables begin?

A variable in Python with a single leading underscore means that it is considered “private” (i.e. not part of public API).

When (either in terms of date, or even commit hash if that is relevant) did this convention begin in Python?

Do the mean the single underscore or double underscore prefix on variables?

Maybe this was in python 1.xx certainly in 2.xx.
It’s is very old… why do you ask?

Ah, good question. I was referring to the single prefixed underscores. Double would also be nice-to-know.

Very old is also what I suspected, too, but I really don’t know myself.

I am asking mostly to gather information that may support (or refute if the facts don’t agree) an argument that I wish to make that this convention is “nothing new” and is “well-established”.

A PEP might be a good reference, if one exists that weighs in on this question.

It predates Python. C libraries also used _* names for private names,
and have done for long before Python was a thing. This is distinct
from “exposed to the linker” names.

And in UNIX system calls, the public function we call call eg open to
open a file itself will itself usually be a thin shim which calls a low
level _open which is the syscall switch-to-the-kernel. That dates from
at least the 70s.

3 Likes

I found this: Ancient Releases | Python.org

It led me to this example: python-0.9.1/lib/Buttons.py at main · smontanaro/python-0.9.1 (github.com)

So even ‘within Python’ it must be almost, if not equally, as old as Python itself.

What you’re saying about the history makes sense. Thank you for sharing it!