# tff.structure.Struct

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |

Represents a struct-like structure with named and/or unnamed fields.

    tff.structure.Struct(
        elements: Iterable[tuple[Optional[str], _T]]
    )

`Struct`s are similar to `collections.namedtuple` in that their elements can
be accessed by name or by index. However, `Struct`s provide a performance
improvement over `collections.namedtuple` by using a single class to
represent values with many different possible structures, rather than
creating a brand new class for every new instance.

`Struct`s are commonly used inside Tensorflow Federated as a standard
intermediate representation of other structure types, including `list`s,
`tuple`s, `dict`s, `namedtuple`s, and `attr.s` classes.

#### Example:

    x = Struct([('foo', 10), (None, 20), ('bar', 30)])

    len(x) == 3
    x[0] == 10
    x[1] == 20
    x[2] == 30
    list(iter(x)) == [10, 20, 30]
    dir(x) == ['bar', 'foo']
    x.foo == 10
    x['bar'] == 30

Note that field names are optional, allowing `Struct` to be used like an
ordinary positional tuple.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `elements` | An iterable of element specifications, each being a pair consisting of the element name (either `str`, or `None`), and the element value. The order is significant. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | if the `elements` are not a list, or if any of the items on the list is not a pair with a string at the first position. |

<br />

## Methods

### `named`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    @classmethod
    named(
        **kwargs
    ) -> 'Struct'

Constructs a new `Struct` with all named elements.

### `unnamed`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    @classmethod
    unnamed(
        *args
    ) -> 'Struct'

Constructs a new `Struct` with all unnamed elements.

### `__eq__`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    __eq__(
        other: object
    ) -> bool

Return self==value.

### `__getitem__`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    __getitem__(
        key: Union[int, str, slice]
    ) -> _T

### `__iter__`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    __iter__() -> Iterator[_T]

### `__len__`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    __len__() -> int

### `__ne__`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    __ne__(
        other: object
    ) -> bool

Return self!=value.