17

Does anyone know where are __file__, __main__, etc defined? And what are they officially called? __eq__ and __ge__ are "magic methods", so right now I'm just referring to them wholly as "magic constants" but I don't even know if that's right.

Google search really isn't turning up anything and even Python's own documentation doesn't seem to have a comprehensive list of them after scanning through the various pages. Any guidance would be much appreciated on this topic.

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 2
    They aren't "magic constants", so not really. Some are related to imports: https://docs.python.org/3/reference/import.html#import-related-module-attributes. `__main__` is here: https://docs.python.org/3/library/__main__.html. The object attributes are in the data model: https://docs.python.org/3/reference/datamodel.html – jonrsharpe Jul 13 '16 at 07:27
  • Try to call `globals()` function and see the result. – sehrob Jul 13 '16 at 07:33
  • Out of curiosity, why do you want such a table? Is it curiosity to know what is there? Are you wanting to define your own and feel worried that you might conflict with something that is already there? If the second case, IIRC, _all_ dunder names are reserved by the interpreter and so you should _never_ define new ones. There are a few which are used so frequently that it is unlikely that the language will ever use them for a different purpose (e.g. `__version__`) -- though it _could_... In any case, you definitely shouldn't go about defining anything new. – mgilson Jul 13 '16 at 07:39
  • @mgilson Pure curiosity and a desire to know whats out there. I like to study things that some others might consider redundant or useless – AlanSTACK Jul 13 '16 at 07:56

1 Answers1

22

Short answer: no. For the longer answer, which got badly out of hand, keep reading...


There is no comprehensive table of those __dunder_names__ (also not their official title!), as far as I'm aware. There are a couple of sources:

  • The only real "magic constant" is __debug__: it's a SyntaxError to attempt to assign to this name. It's covered in the list of constants and mentioned in the context of the assert statement.

  • Another module-level name with specific use by a statement is __all__, which is documented alongside the import statement.

  • There are two special modules, documented in the library reference, which have their own pages:

    • __main__ is the top-level environment in which a script is executed.

    • __future__ is for accessing language features that aren't yet mandatory (e.g. print_function to replace the print statement in Python 2).

  • Most of the rest (__name__, __file__, etc.) are added to modules by the import system, so are listed in the import documentation.

There are also many related to objects. The basic methods for implementing built-in behaviour (like __eq__ and __ge__, as you mention) are listed in the data model documentation. But plenty of other, more specific names exist; for example, there are several related specifically to exceptions, like __cause__ and __traceback__, in the exceptions documentation.


Note that there is nothing particularly "magic" about most of these, they are just regular attributes and can be assigned to as you see fit. However, they are considered reserved for internal Python machinery, so you shouldn't add your own; per the language reference on "reserved classes of identifiers":

Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.

That said, there are a couple in common use that I don't think are actually specified anywhere in the official docs, like __author__ and __version__; see e.g. What is the common header format of Python files? and What is the origin of __author__? A few have semi-official status via PEP-8, but that's about it.


A few others have trodden this path, by the looks of it:

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    There are also some in [here](https://docs.python.org/3/library/types.html#types.ModuleType) – 301_Moved_Permanently Jul 13 '16 at 07:38
  • @MathiasEttinger true, but they are all covered in either the data model (`__doc__`) or the import docs. – jonrsharpe Jul 13 '16 at 07:40
  • So since there isnt a comprehensive list, is there at least somewhere (or perhaps you would like to provide them) that gives just the couple of main ones. As a newcomer to python, it would sure be nice to have some sort of table, even if it is incomplete. Or am I doomed to play scavenger hunt? – AlanSTACK Jul 13 '16 at 07:46
  • 2
    @Alan *"is there at least somewhere ... that gives just the couple of main ones"* - you mean like the resources I'm linking to in the answer? – jonrsharpe Jul 13 '16 at 07:48
  • Ah, alright. I was just making sure, you must understand the uneasiness of not having the same level of experience as someone like yourself. So would you say that these are basically the primary ones that I will ever have to worry about, or do you have any other off the top of your head that I should note down? – AlanSTACK Jul 13 '16 at 07:50
  • 2
    @Alan to be honest, I think that most of these you will find out about when you need to know them. As the rule is not to invent *any* of your own, you won't have any clashes. – jonrsharpe Jul 13 '16 at 07:54