Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a comprehensive table of Python's "magic constants"?

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 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 layers of pages.

like image 838
AlanSTACK Avatar asked Jul 13 '16 07:07

AlanSTACK


People also ask

How many magic constants are in PHP?

There are nine magic constants in PHP. In which eight magic constants start and end with double underscores (__). All of the constants are resolved at compile-time instead of run time, unlike the regular constant.

What are PHP Magic constants explain in detail?

Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are created by various extensions. There are nine magic constant in the PHP and all of the constant resolved at the compile-time, not like the regular constant which is resolved at run time.

What is Python magic variable?

Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. Few examples for magic methods are: __init__, __add__, __len__, __repr__ etc.

Which of the following is not a magic predefined constant?

The __DIR__ magic constant is used to return the complete directory path. 9) Which of the following are not predefined magic constant? Explanation: The __PATH__ is not predefined magic constant in PHP.


1 Answers

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:

  • Finding a list of all double-underscore variables?
  • I need __closure__
  • Built-in magic variable names/attributes
like image 166
jonrsharpe Avatar answered Oct 05 '22 23:10

jonrsharpe