Why does Python use "__" (double underscore) for Python Defined Names?

Why does Python use “__” (double underscore) for Python Defined Names?

I am not used to it…

Regards,
Albert Yu

GvR wanted a way to mark such names and he was influenced by the convention of the C language in which he wrote Python.

In hindsight, it could have been “_” (single underscore), and that might have been preferable as it’s not always clear that it’s, say, __init__ and not _init_ (those new to the language sometimes get it wrong), but it’s much too late to change it now!

2 Likes

On the flipside, a double underscore reduces the change of collision, is easier to spot such names at a glance, and allows for single underscores to be given another level of project-defined semantic meaning, much like __ vs. _-prefixed names.