Hello.py
The disassembled code will be displayed with line numbers. Each line represents a bytecode instruction and tells the interpreter what to do. For example, in the given disassembled code:
def myfunc(): print("ahmet")
Bytecode
4 0 LOAD_GLOBAL 0 (print) 2 LOAD_CONST 1 ('ahmet') 4 CALL_FUNCTION 1 6 POP_TOP 8 LOAD_CONST 0 (None) 10 RETURN_VALUE
Mnemonics | Description | Source Code |
---|---|---|
LOAD_GLOBAL | Loads the global named co_names[/namei/] onto the sta | |
CALL_FUNCTION | Calls a function. The low byte of /argc/ indicates the number of positional parameters, the high byte the number of keyword parameters. On the stack, the opcode finds the keyword parameters first. For each keyword argument, the value is on top of the key. Below the keyword parameters, the positional parameters are on the stack, with the right-most parameter on top. Below the parameters, the function object to call is on the stack. | |
POP_TOP | Removes the top-of-stack (TOS) item | |
LOAD_COST | Pushes "co_consts[/consti/]" onto the stac |
For all opcodes link
If you have Docker, you can quickly spin up a Python 3.13 shell to play around with the examples in this article like so:
$ docker run -it --rm python:3.13
Not using Docker? We recommend installing Python 3.13 with pyenv:
$ pyenv install 3.13.0
Follow the instuctions on the link
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zprofile
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zprofile
mypy
is a static type checker for Python. Its primary purpose is to ensure that Python code adheres to type annotations, helping developers catch type-related errors before runtime. Here's a breakdown of mypy
's usage and benefits:
mypy
analyzes type hints in your Python code without actually running the code.- It catches type inconsistencies, like trying to assign a string to a variable that’s annotated as an integer, providing errors or warnings about these issues.
- By enforcing type consistency,
mypy
helps identify potential bugs that could otherwise lead to runtime errors. - For example, if a function expects an argument of type
List[int]
and you passList[str]
,mypy
will catch the mismatch.
- Type annotations make code easier to understand for both developers and tools by documenting what types of data are expected.
mypy
enforces these annotations, helping you maintain consistent and well-documented code.
- Install : You can install
mypy
using pip:
bashCopy codepip install mypy
- Run : Run
mypy
on your Python files:
bashCopy codemypy my_script.py
The -m
parameter in Python allows you to run a module as a script directly from the command line, rather than running a file. Here’s a breakdown of what it does, why it's useful, and examples of when you might use it:
- When you use
python -m <module_name>
, Python will locate and run the specified module as a script. - This works by searching for the module in the Python environment’s path (using
sys.path
) and running the module's__main__
block if it exists.
- Module-based Execution : It enables you to run standard library modules and custom modules in your Python environment without specifying the full path.
- Path Management :
-m
lets Python know to look for the module in the package path rather than treating the name as a script path. This avoids issues withPYTHONPATH
and is particularly useful for packages with submodules. - Testing and Running Tools : Many tools and libraries provide
-m
functionality to easily run specific tasks, like unit tests or installing packages.
python -m http.server
: Starts a simple HTTP server.python -m venv myenv
: Creates a virtual environment in themyenv
directory.python -m unittest
: Runs the Python unit test framework.
python -m pip install <package>
: Runs thepip
package manager.python -m mymodule.submodule
: Useful when you want to run a specific module or script within a package or a virtual environment.
-
If you have a package structure like this:
cssCopy codemypackage/ ├── __init__.py └── main.py
You can run
main.py
withpython -m mypackage.main
instead of specifying the path directly.