Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
As well as magic methods, many Python objects have magic attributes. These are attributes whose names start and end with double underscores and that have a special meaning to the Python interpreter. Table B.12 lists the most common of these attributes along with their description.
| Name | Description |
|---|---|
| __doc__ | Most Python objects have a __doc__ attribute that holds the docstring for the object. If there is no docstring, this attribute will be None. Docstrings are displayed by the help function and used by automated documentation tools. Docstrings can be assigned directly or created with a string literal as the first entry (before any code) in a module, class, function, or method. |
| __dict__ | __dict__ is a read-only attribute that holds all members of an object. It forms the object namespace. It exists for all objects except for those that use __slots__. |
| __slots__ | When __slots__ is set as a class variable (a list of strings), it reserves space in the class for the named members. Instances of classes with __slots__ will not have a __dict__ dictionary, and attempting to set an attribute not listed in __slots__ will raise an AttributeError. It is intended as a memory optimization, saving the space required for a dictionary per instance. In IronPython classes that define __slots__ are heavily optimised; attribute lookup is approximately 4x faster. __slots__ has several caveats about its use. See the Python documentation for more details.[a] |
| __class__ | A reference to the class for all class instances (everything in Python is an instance of a class). |
| __bases__ | A class attribute referencing a tuple of the base classes for the class. |
| __name__ | An attribute (string) on modules, functions, and classes. It is assignable, allowing decorated functions to retain the same name as the function they wrap (very useful for tracebacks from decorated functions). |
| __all__ | An optional attribute (list of strings) for modules. If available, this lists all the names that will be exported by the module when from module import * is executed. Defining __all__ can be a useful way of defining the public API of a module. |
| __file__ | A module attribute (string) containing the path the module was loaded from on the filesystem. This attribute does not exist for built-in modules. |
| __module__ | A class, function, and method attribute (string) with the name of the module the object was defined in. |
| __metaclass__ | If this is defined as a class attribute, then the callable assigned will be called to create the class instead of type. It can also be used as a module-level attribute, and all old-style classes (without an explicit alternative metaclass or inheriting from a class with a metaclass) will use the callable assigned as their metaclass. A quick way to convert all old-style classes in a module into new-style classes is to add __metaclass__ = type at the start of the module. (It doesn’t affect new-style classes, as they inherit type as a metaclass from object.) |
| __debug__ | A global variable (Boolean) indicating whether the interpreter is being run with optimizations on (-O or –OO command-line switches). |