Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a list of all double-underscore variables?

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.

like image 968
Michael0x2a Avatar asked Jan 19 '12 02:01

Michael0x2a


People also ask

How do you find the double underscore variable in Python?

In Python, we use double underscore i.e., __ before the attribute's name and those attributes will not be directly accessible/visible outside. Double underscore mangles the attribute's name. However, that variable can still be accessed using some tricky syntax but it's generally not a good idea to do so.

What is a double underscore called?

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

What are __ variables in Python?

__var__ : double leading and trailing underscore variables (at least two leading and trailing underscores). Also called dunders. This naming convention is used by python to define variables internally. Avoid using this convention to prevent name conflicts that could arise with python updates.

What are Dunder variables?

A dunder variable is a variable that Python has defined so that it can use it in a “Special way”. This Special way depends on the variable that is being used. Note: For more information, refer to Dunder or magic methods in Python.


1 Answers

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__' 
like image 149
Raymond Hettinger Avatar answered Oct 07 '22 23:10

Raymond Hettinger