Python - Magic or Dunder Methods
In Python, special methods
are also called magic methods
, or dunder methods
. This latter terminology, dunder, refers to a particular naming convention that Python uses to name its special methods and attributes.
Python magic methods, also known as dunder methods
(short for "double underscore"), are special methods that Python uses to perform various operations. They are called "magic" ==because they are invoked implicitly by the Python interpreter in response to specific operations on objects=.
Special Method | Description |
---|---|
.__init__() |
Provides an initializer in Python classes |
.__str__() and .__repr__() |
Provide string representations for objects |
.__call__() |
Makes the instances of a class callable |
.__len__() |
Supports the len() function |
__main__
In Python, main is not a magic method but rather a special name for the namespace in which the top-level script executes. When a Python file is run directly as a script (not imported as a module), the Python interpreter sets the special variable name to have the value "main" within that script's namespace.
Here’s how it typically works:
-
Main Script Execution: When you run a Python script directly by calling python script.py from the command line, Python sets name to "main" for that script.
-
Module Import: If a script is imported as a module by another script using import, then name is set to the name of the module (e.g., "module_name"), not "main".
This mechanism allows Python scripts to distinguish between being run as the main program versus being imported as a module into another program. This distinction is often used to include or exclude certain blocks of code depending on how the script is being used.
__name__
built-in variable
In Python, __name__
is a special built-in variable. Its value depends on how and where the Python interpreter is running the code.
-
When Python runs a script directly:
- If a Python script is executed directly using python script.py from the command line or by clicking on it in an IDE, Python sets
__name__
to "main" in that script's namespace. -
This allows the script to execute certain code only if it is being run as the main program and not when it is imported as a module into another script.
-
When you run python
script.py
, the condition__name__
== "main" evaluates to True, so main() function will be called and "This is the main function." will be printed.
- If a Python script is executed directly using python script.py from the command line or by clicking on it in an IDE, Python sets
-
When Python imports a module:
- If a Python script is imported as a module into another script using import script, Python sets
__name__
to "script" in the imported script's namespace. -
This allows the imported script to define functions, classes, and variables without executing the main logic that may be intended only for direct execution.
-
Here,
__name__
inscript.py
will be "script", so the code inside the if__name__
== "main": block will not execute when script.py is imported as a module.
- If a Python script is imported as a module into another script using import script, Python sets
Summary:
__name__
== "main": Used to check if a script is being run directly by the Python interpreter.__name__
== "<module_name>
": Used when a script is imported as a module into another script, indicating its module name.
By using __name__
, Python scripts can be designed to behave differently based on whether they are the main program or a module being imported, providing flexibility in organizing and executing Python code.