In addition to a large number of “special methods” that control object behavior, Python also provides a series of special attributes that describe the meta-information of modules, functions, instances, and classes.
These attributes typically appear in the form of __name__, __doc__, __dict__, etc., and are the foundation for introspection, debugging, reflection, and even framework design.
I. Special Attributes of Modules
Module Special Attributes
Module objects are created by the interpreter upon import, carrying a series of predefined attributes:
__name__: Module name
__doc__: Module documentation string
__file__: Path to the module’s source file (built-in modules may not have this attribute)
__package__: Name of the package to which the module belongs
__loader__: Loader object that imported this module
__spec__: Import specification of the module (ModuleSpec object)
__cached__: Path to the bytecode cache file (.pyc)
__builtins__: Reference to the built-in namespace
__all__: List of explicitly exported symbols (for use with from module import *)
__path__: Specific to package modules, used for searching submodules within the package
For more details, see:
“Python: Special Attributes of Modules“
II. Special Attributes of Functions
Function Special Attributes
Function objects are automatically generated by Python at the time of definition, possessing attributes that describe signature, closure, and code information.
func.__name__: Function name
func.__doc__: Function documentation string
func.__module__: Name of the module where the function is defined
func.__qualname__: Qualified name of the function (including class name/nesting level)
func.__defaults__: Tuple of default values for positional parameters
func.__kwdefaults__: Dictionary of default values for keyword parameters
func.__annotations__: Annotations for parameters and return values
func.__code__: Code object of the function (bytecode, variable names, etc.)
func.__globals__: Global namespace where the function is defined
func.__closure__: Tuple of closure cells (references to external variables)
func.__dict__: Dictionary of custom attributes for the function object
For more details, see:
“Python: Special Attributes of Functions“
III. Special Attributes of Instances (Objects)
Instance Special Attributes
Instances are products of classes and typically have the following key attributes at runtime.
obj.__dict__: Dictionary storing instance attributes (may not exist if the class defines __slots__)
obj.__class__: Class object to which the instance belongs
obj.__doc__: Documentation string (inherited from the class by default)
obj.__module__: Name of the module defining the class to which this instance belongs
obj.__weakref__: Stores weak reference information for the object
For more details, see:
“Python: Special Attributes of Instances (Objects)“
IV. Special Attributes of Classes
Class Special Attributes
Classes themselves are instances of type and, as “meta-objects”, also possess a large number of attributes.
cls.__name__: Class name
cls.__qualname__: Qualified name of the class (including nesting path)
cls.__module__: Name of the module where the class is defined
cls.__doc__: Class documentation string
cls.__bases__: Tuple of base classes
cls.__mro__: Method Resolution Order (MRO)
cls.__dict__: Namespace dictionary of the class (stores methods and attributes)
cls.__annotations__: Dictionary of type annotations for class variables
cls.__weakref__: Supports weak references to class objects
cls.__slots__: If defined, limits the set of instance attributes and saves memory
For more details, see:
“Python: Special Attributes of Classes“
📘 Summary
Module attributes: Provide information about name, path, and import mechanism.
Function attributes: Reveal signature, closure, and code object.
Instance attributes: Record object state and its belonging class.
Class attributes: Reflect inheritance structure, namespace, and weak reference mechanism.
Special attributes describe “what an object is”, while special methods define “what an object can do”. Together, they form the core of the Python object model.
“Likes have good intentions, appreciation is encouragement“