0

What is the purpose of python variables __something__ e.g. __name__, __file__

I often see them in real python programs. Is there a list of these variables?

thank you

xralf
  • 3,312
  • 45
  • 129
  • 200
  • 2
    See this question & answer: http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python – Mike Cialowicz Sep 30 '11 at 14:59
  • 1
    `__main__` doesn't belong there -- it's `if __name__ == '__main__':` so it's `__name__` that is special like `__file__` etc., not `__main__`. – agf Sep 30 '11 at 14:59
  • Aren't they a naming convention for internal variables to python modules/libraries? Like _variableName in c# – Oliver Sep 30 '11 at 15:00
  • 3
    Or this question and answer http://stackoverflow.com/questions/3443043/why-does-python-use-two-underscores-for-certain-things – Circumflex Sep 30 '11 at 15:00
  • 1
    @MikeCialowicz That definitely isn't the same question and I don't think the answers really address this at all. – agf Sep 30 '11 at 15:00
  • @Oliver The conventions are for `_foo` or name mangling is `__foo`, `__foo__` is different. – agf Sep 30 '11 at 15:03
  • @Farmer_Joe good link; this is an exact duplicate and that question's answers cover all the bases and have links to the relevant docs. – agf Sep 30 '11 at 15:04
  • @agf sorry confused between Mike and Joe links – Mansuro Sep 30 '11 at 15:15

3 Answers3

5

PEP8 covers this:

In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

  • _single_leading_underscore: weak "internal use" indicator. E.g. "from M import *" does not import objects whose name starts with an underscore.

  • single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

    Tkinter.Toplevel(master, class_='ClassName')

  • __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.

The final bullet point is pertinent.

Is there a list of these variables?

I doubt that there is an official list of all such double leading and trailing underscore names, but I could not say so for sure.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    They're mostly on the http://docs.python.org/dev/reference/datamodel.html page, either under "Special Method Names" or "The Standard Type Hierarchy" for `__file__` and `__name__` -- I don't see `__all__` anywhere on that page though. – agf Sep 30 '11 at 15:07
1

Check this chapter of Python manual. Does it satisfy you?

Griwes
  • 8,805
  • 2
  • 43
  • 70
  • 2
    Please don't just provide a link, provide some context, and make the link test relevant like "Check out this chapter of the Python docs about the Python Data Model". – agf Sep 30 '11 at 15:01
1

They're considered special or magic. The language doesn't prevent you from creating your own, but don't.

Here is a good guide on the magic methods but there are also special attributes, such as __all__, which is normally pronounced "dunder all" or "under under all".

Dan
  • 3,665
  • 1
  • 31
  • 39