Special Attributes & Methods

Contents

1. Special Attributes & Methods#

1.1. Introduction#

In Python, all data types have a set of special attributes, commonly referred to as dunder attributes[1].
These attributes provide developers with useful metadata about Python objects. They follow the naming pattern __attribute__ — with two leading and two trailing underscores where attribute is the name of the given dunder attribute.

Python’s built-in objects come with many such attributes. Some external libraries also define their own custom dunder attributes, but this practice is generally discouraged unless it is truly necessary and thoroughly documented [Python Software Foundation, 2025].

In this section, we will explore the most important core dunder attributes across different types of Python objects.

1.2. Of Modules#

1.2.1. Attributes#

1.2.1.1. __name__#

The __name__ attribute holds the name of a module.

What is a Python module?

Not sure what a Python module is? According to the official Python tutorial, a module is simply
“[…] a file containing Python definitions and statements” [Python Software Foundation, 2025].

By default, the module name corresponds to the filename without the .py suffix [Python Software Foundation, 2025].

For example, consider the file my_math.py

# file my_math.py

def multiply(a: int, b: int) -> int:
  return a * b

Now, if we import this module:

import my_math
print(my_math.__name__)

The output will be:

my_math

However, there is a special case: the __mame__ attribute takes the value: __main__ when a module is executed directly [Python Software Foundation, 2025]. This covers a few cases [Python Software Foundation, 2025]:

1.2.1.1.1. Running from interactive shell (Python REPL, IPython),#
print( __name__)
__main__
1.2.1.1.2. Running from source file:#

Let’s modify my_math.py to display the value __main__.

# file my_math.py
print(__name__)

def multiply(a: int, b: int) -> int:
  return a * b

If we now run:

python3 my_math.py

The output will be:

__main__
1.2.1.1.3. Running directly code text#

The same happens when you execute code passed as a string:

python -c "print(__name__)"

Output:

__main__
1.2.1.1.4. Running code passed from standard input:#
echo "print(__name__)" | python

Output:

__main__

Did you know?

Because __name__ is set to __main__ only when a file is executed directly, you will often see Python code guarded like this:

if __name__ == "__main__":
    ...

This pattern allows developers to include logic that should only run on direct execution, while preventing it from running when the file is imported as a module. Let’s investigate with an example:

Now compare the two cases:

  • running python my_math.py will print I'm executed directly!

  • importing it inside a Python shell (or any module) with import my_math will print nothing

1.2.1.2. __spec__#

The __spec__ attribute is an instance of ModuleSpec.
It contains the specification for the module and is central to the import system.
Some of its most relevant attributes are listed below (Table 1.1):

Table 1.1 Some of the main attributes of the ModuleSpec#

Attribute

Meaning

name

The module’s fully qualified name.

origin

Path to the file (.py) where the module is defined.
May be None (e.g., for namespace packages; see [Smith, 2012].

loader

The Loader implementation

cached

The path of the compiled module’s code (can be None)

Did you know?

For code executed directly (see __name__), __spec__ is usually None python-ref-import-mainspec.
There are two main exceptions:

  1. When running as a module using the -m option:

    python -m my_module
    
  2. When executing a directory or a zip file containing a __main__.py file:

    Example project structure:

    my_project/
    ├── __main__.py
    └── utils.py
    

    Contents of __main__.py:

    import sys
    
    print(f"Running {__name__=}")
    print(f"{__spec__=}")
    

    Running the directory as a script:

    python my_project
    

    Output (simplified):

    Running __name__='__main__'
    __spec__=ModuleSpec(name='__main__', loader=..., origin='my_project/__main__.py')
    

1.2.1.3. __package___#

1.2.1.4. __loader__#

1.2.1.5. __path__#

1.2.1.6. __annotations__#

1.2.1.7. __dict__#

1.2.2. Methods#

1.2.2.1. ….#

1.3. Of Functions#

1.3.1. Attributes#

1.3.1.1. __globals__#

1.3.1.2. __closure__#

1.3.1.3. __doc__#

1.3.1.4. __name__#

1.3.1.5. __qualname__#

1.3.1.6. __defaults__#

1.3.1.7. __code__#

1.3.1.8. __dict__#

1.3.1.9. __annotations__#

1.3.1.10. __kwdefaults__#

1.3.1.11. __type_params__#

1.3.2. Methods#

1.3.2.1. ….#

1.4. Of Methods#

1.4.1. Attributes#

1.4.1.1. __self__#

1.4.1.2. __func__#

1.4.2. Methods#

1.4.2.1. ….#

1.5. Of Classes#

1.5.1. Attributes#

1.5.1.1. __mro__#

1.6. Of Objects#

1.6.1. Attributes#

1.6.1.1. __module__#

1.6.1.2. __class__#

1.6.1.3. __doc__#

1.6.1.4. __dict__#

1.6.1.5. __firstlineno__#

1.6.1.6. __static_attributes__#

1.6.1.7. __weakref__#

1.6.2. Methods#

1.6.2.1. __new__()#

1.6.2.2. __init__()#

1.6.2.3. __setattr__()#

1.6.2.4. __getattribute__()#

1.6.2.5. __delattr__()#

1.6.2.6. __getstate__()#

1.6.2.7. __dir__()#

1.6.2.8. __eq__()#

1.6.2.9. __gt__()#

1.6.2.10. __ge__()#

1.6.2.11. __lt__()#

1.6.2.12. __le__()#

1.6.2.13. __sizeof__()#

1.6.2.14. __hash__()#

1.6.2.15. __reduce__()#

1.6.2.16. __reduce_ex__()#

1.6.2.17. __repr__()#

1.6.2.18. __str__()#

1.6.2.19. __format__()#

1.6.2.20. __subclasshook__()#

Everything is an Object in Python

If you are already experienced in Python, you probably know that literally all data in the Python ecosystem are objects [Python Software Foundation, 2025]. This means that modules, functions, methods, and classes are themselves objects, not just instances of classes (that, by the way, are objects too).

In this section, the “Of Objects” subsection covers the core special attributes that are fundamental to all Python objects. The previous subsections (Modules, Functions, Methods, Classes) describe additional specialized attributes specific to those object types. Therefore, objects of those types will have both the general object attributes listed here and their type-specific attributes described above.