34

Related: What is the common header format of Python files?

Where can I find a list of all double-underscore variables that are commonly used in Python?

In Python, variables starting and ending with double underscores are typically to store metadata or are built into the system. For example,

#!/usr/bin/env python

__author__ = 'Michael0x2a'
__license__ = 'GPL'

class Test(object):
    def __init__(self):
        print 'Hello World!'

if __name__ == '__main__':
    t = Test()

I'm pretty certain __author__ and __license__ are pretty well known. What other double-underscore metadata variables are there? Is there a comprehensive list I can check somewhere? Can I just make up my own, or are there a bunch of ones that have become de-facto standards that I should use?

Things like __init__, __name__, and __doc__ are pretty much built into Python. Are those the only reserved double-underscore variables? Are there any more? Is there some place I can get a list?

[Edit]
I was browsing and encountered another question that linked to a mindmap of a bunch of double-underscore variables.

wovano
  • 4,543
  • 5
  • 22
  • 49
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224

3 Answers3

41

If you want to see magic names whether documented or not, go to the Lib directory and run:

egrep -oh '__[A-Za-z_][A-Za-z_0-9]*__' *.py | sort | uniq

That produces:

'__all__'
'__args__'
'__author__'
'__bases__'
'__builtin__'
'__builtins__'
'__cached__'
'__call__'
'__class__'
'__copy__'
'__credits__'
'__date__'
'__decimal_context__'
'__deepcopy__'
'__dict__'
'__doc__'
'__exception__'
'__file__'
'__flags__'
'__ge__'
'__getinitargs__'
'__getstate__'
'__gt__'
'__import__'
'__importer__'
'__init__'
'__ispkg__'
'__iter__'
'__le__'
'__len__'
'__loader__'
'__lt__'
'__main__'
'__module__'
'__mro__'
'__name__'
'__package__'
'__path__'
'__pkgdir__'
'__return__'
'__safe_for_unpickling__'
'__setstate__'
'__slots__'
'__temp__'
'__test__'
'__version__'
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 9
    Interestingly, there are dozens of well-known dunder names that aren't in this list: __add__, __iadd__, __radd__, etc. – Ned Batchelder Jan 24 '12 at 00:54
  • 9
    @NedBatchelder To get a more complete list, you need to recursively search __Lib__ *and* the directories below it: ``find Lib -name '*.py' -exec egrep -oh '__[A-Za-z_][A-Za-z_0-9]*__' '{}' \; | sort | uniq``. You'll see that there are *many* more names -- 256 of them on my Py27 checkout :) – Raymond Hettinger Jan 24 '12 at 02:13
14

The complete list used by Python is given in the Python Language Reference section 3, "Data model". Every other one is non-standard or used by third-party modules and is documented separately.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

when i use

dir(object)

i got these:

'__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__'

and i think they are the dunder names every object will have in python

wjandrea
  • 28,235
  • 9
  • 60
  • 81
atongsa
  • 334
  • 3
  • 10