This page provides examples of different FIDL types and how they are
instantiated and used in Python.

Most of the examples focus on how types are translated from FIDL to Python.
If you would like to learn about implementng FIDL servers or handling
async code, check out the [tutorial](https://fuchsia.dev/docs/development/tools/fuchsia-controller/scripting-remote-actions) or the
[async Python best practices](https://fuchsia.dev/docs/development/tools/fuchsia-controller/async-python) page.

## Bits and Enum types

Both bits and enum types are represented the same in Python, so this example
works for both.

### FIDL

    library test.bits;

    type MyBits = strict bits : uint32 {
        MY_FIRST_BIT = 1;
        MY_OTHER_BIT = 2;
        // check for any collisions between the bitmask and a "mask" member
        MASK = 4;
    };

### Python

    import fidl_test_bits

    a = fidl_test_bits.MyBits.MY_FIRST_BIT
    b = fidl_test_bits.MyBits(0b10)
    c = fidl_test_bits.MyBits(0b10000101)

    print(f"{a!r}")
    print(f"{b!r}")
    print(f"{c!r}")

This Python code above prints the following output:

    <MyBits.MY_FIRST_BIT: 1>
    <MyBits.MY_OTHER_BIT: 2>
    <MyBits.MY_FIRST_BIT|MASK|128: 133>

## Struct types

In Python, size limits are not enforced during construction, so if you were to
make a struct with an overly long string, for example, the example below would
not create an error for the user until attempting to encode the struct to bytes.
This goes for other composited structures as well, such as tables and unions.

### FIDL

    library test.struct;

    type Simple = struct {
        f1 uint8;
        f2 bool;
    };

    type BasicStruct = struct {
        x uint32;
        y string;
    };

### Python

    import fidl_test_struct

    d = fidl_test_struct.Simple(f1=5, f2=True)

    print(f"{d!r}")

This Python code above prints the following output:

    Simple(f1=5, f2=True)

If you were to attempt to construct this type without one of the fields, it
would raise a TypeError exception listing which arguments were missing.

## Table types

These are identical to struct types but without the strict constructor
requirements. Any fields not supplied to the constructor will be set to `None`.

### FIDL

    library test.table;

    type EmptyTable = table {};

    type SimpleTable = table {
        1: x int64;
        5: y int64;
    };

### Python

    import fidl_test_table

    e = fidl_test_table.EmptyTable()
    f = fidl_test_table.SimpleTable(x=6)
    g = fidl_test_table.SimpleTable(y=7)
    h = fidl_test_table.SimpleTable()

    print(f"{e!r}")
    print(f"{f!r}")
    print(f"{g!r}")
    print(f"{h!r}")

This Python code prints the following output:

    EmptyTable()
    SimpleTable(x=6, y=None)
    SimpleTable(x=None, y=7)
    SimpleTable(x=None, y=None)

## Union types

These work similar to struct types but only one variant can be specified.
A flexible union can support no variant being specified.

### FIDL

    library test.union;

    type Pizza = struct {
        toppings vector<string:16>;
    };

    type Pasta = struct {
        sauce string:16;
    };

    type PizzaOrPasta = strict union {
        1: pizza Pizza;
        2: pasta Pasta;
    };

### Python

    import fidl_test_union

    i = fidl_test_union.PizzaOrPasta(
        pizza=fidl_test_union.Pizza(toppings=["pepperoni", "jalapeños"])
    )
    j = fidl_test_union.PizzaOrPasta(pasta=fidl_test_union.Pasta(sauce="pesto"))

    print(f"{i!r}")
    print(f"{j!r}")

This Python code prints the following output:

    <'PizzaOrPasta' object(pizza=Pizza(toppings=['pepperoni', 'jalapeños']))>
    <'PizzaOrPasta' object(pasta=Pasta(sauce='pesto'))>

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/tools/fuchsia-controller/fidl-bindings.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/tools/fuchsia-controller/fidl-bindings.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/tools/fuchsia-controller/fidl-bindings.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/tools/fuchsia-controller/fidl-bindings.md "Edit the source code of this page")