8

What does the double underscore indicate in python? I remember reading a tutorial that said it had something to do with a hidden variable, but I feel like there is more to it than that and I keep seeing examples of code that have double underscores and I don't understand what it means.

david
  • 6,303
  • 16
  • 54
  • 91

1 Answers1

8

From PEP 8:

  • __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).

  • __double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Good reply. But this explains better (and also refers to the PEP 8 style guide, too): [The meaning of a single- and a double-underscore before an object name in Python](http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python) – paulsm4 Oct 07 '13 at 04:14
  • 1
    @paulsm4, strangely enough I've left a number of upvotes on that question and its answers already. As SO grows it becomes difficult to remember all the good information it contains. – Mark Ransom Oct 07 '13 at 04:28