# How to handwrite Python Bytecode

**URL:** <https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366>\
**Category:** Python Help\
**Tags:** help\
**Created:** [February 25, 2021, 2:22am UTC](https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366 "2021-02-25T02:22:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glubs9](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@glubs9](https://discuss.python.org/u/glubs9)\
**Post date:** [February 25, 2021, 2:22am UTC](https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366/1 "2021-02-25T02:22:08Z")

</div>

Hi!, I am a long time user of python (though this is my first time in the community) and wanted to understand python bytecode better (and potentially compile some languages to python bytecode) but I’m really struggling to understand how the actual bytecode file is structured? I did read through the dis library (and some other tutorials) on python bytecode so I understand how it works but they don’t really cover the actual python bytecode file itself. I thought to understand what is going on it would be good to handwrite some python bytecode but so far I am very much struggling.

If anybody could show me a resource or explain it to me or explain why I could never do this it would be great!

Thanks in advance.

(also sorry if this is the wrong place to post a question like this, I am very new to this community!)

(This question was originally asked in core development, and I was advised that the Users board might be a better place to look.)

---

<div class="post-metadata">

**Author:** ![Blackward](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@Blackward](https://discuss.python.org/u/Blackward)\
**Post date:** [April 18, 2021, 2:47am UTC](https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366/2 "2021-04-18T02:47:11Z")

</div>

Hi glubs,

bytecode differs for different Python versions - which is why it is not sensible to provide just the bytecode normally…

If you want to “compile” other languages to Python, you should not compile to bytecode but to plain Python code instead - if you ask me 🙂 🙃

Cheers, Dominik

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [April 19, 2021, 7:27am UTC](https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366/3 "2021-04-19T07:27:14Z")

</div>

The exact byte-code generated is not documented and subject to change

without notice.

Aside from the dis module (disassembler), there is no documentation for

what byte-code will be generated by any Python code.

Python .pyc files are written using the `marshal` module, so you could

look at that.

[https://docs.python.org/3/library/marshal.html](https://docs.python.org/3/library/marshal.html)

Also there are a few third-party libraries for writing byte-code:

> **[Search results](https://pypi.org/search/?q=bytecode)**
>
> The Python Package Index (PyPI) is a repository of software for the Python programming language.

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [April 19, 2021, 4:41pm UTC](https://discuss.python.org/t/how-to-handwrite-python-bytecode/7366/4 "2021-04-19T16:41:43Z")

</div>

If you want to experiment with how Python’s bytecode works, perhaps you’ll find one of my packages interesting:

> **[brandtbucher/hax](https://github.com/brandtbucher/hax)**
>
> Write compiled bytecode inline with pure Python. 🤖

It basically allows you to “inline” some bytecode instructions within any function (sort of similar to how `asm` extensions can be used with certain C compilers). It’s nice because you can rewrite _part_ of a function in raw bytecode, while using normal Python syntax for other parts.

Just to give you a taste, here’s an example from the README. It accepts a sequence of items, and returns a list with each item repeated twice:

```python
def doubled(items):
    out = []
    for item in items:
        out += item, item
    return out

```

With `hax`, you can do something like this, which keeps `out` on the stack (instead of in a local variable) and uses the `LIST_APPEND` op to build it:

```python
from hax import *

@hax
def doubled(items):

    BUILD_LIST(0)

    for item in items:

        LOAD_FAST("item")
        DUP_TOP()
        LIST_APPEND(3)
        LIST_APPEND(2)

    RETURN_VALUE()

```
