tff.structure.Struct
Stay organized with collections
Save and categorize content based on your preferences.
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.
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.
|
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.
|
Methods
named
View source
@classmethod
named(
**kwargs
) -> 'Struct'
Constructs a new Struct
with all named elements.
unnamed
View source
@classmethod
unnamed(
*args
) -> 'Struct'
Constructs a new Struct
with all unnamed elements.
__eq__
View source
__eq__(
other: object
) -> bool
Return self==value.
__getitem__
View source
__getitem__(
key: Union[int, str, slice]
) -> _T
__iter__
View source
__iter__() -> Iterator[_T]
__len__
View source
__len__() -> int
__ne__
View source
__ne__(
other: object
) -> bool
Return self!=value.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-20 UTC.
[null,null,["Last updated 2024-09-20 UTC."],[],[],null,["# tff.structure.Struct\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [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) |\n\nRepresents a struct-like structure with named and/or unnamed fields. \n\n tff.structure.Struct(\n elements: Iterable[tuple[Optional[str], _T]]\n )\n\n`Struct`s are similar to `collections.namedtuple` in that their elements can\nbe accessed by name or by index. However, `Struct`s provide a performance\nimprovement over `collections.namedtuple` by using a single class to\nrepresent values with many different possible structures, rather than\ncreating a brand new class for every new instance.\n\n`Struct`s are commonly used inside Tensorflow Federated as a standard\nintermediate representation of other structure types, including `list`s,\n`tuple`s, `dict`s, `namedtuple`s, and `attr.s` classes.\n\n#### Example:\n\n x = Struct([('foo', 10), (None, 20), ('bar', 30)])\n\n len(x) == 3\n x[0] == 10\n x[1] == 20\n x[2] == 30\n list(iter(x)) == [10, 20, 30]\n dir(x) == ['bar', 'foo']\n x.foo == 10\n x['bar'] == 30\n\nNote that field names are optional, allowing `Struct` to be used like an\nordinary positional tuple.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|-------------------------------------------------------------------------------------------------------------------------|\n| `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. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `named`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n @classmethod\n named(\n **kwargs\n ) -\u003e 'Struct'\n\nConstructs a new `Struct` with all named elements.\n\n### `unnamed`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n @classmethod\n unnamed(\n *args\n ) -\u003e 'Struct'\n\nConstructs a new `Struct` with all unnamed elements.\n\n### `__eq__`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n __eq__(\n other: object\n ) -\u003e bool\n\nReturn self==value.\n\n### `__getitem__`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n __getitem__(\n key: Union[int, str, slice]\n ) -\u003e _T\n\n### `__iter__`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n __iter__() -\u003e Iterator[_T]\n\n### `__len__`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n __len__() -\u003e int\n\n### `__ne__`\n\n[View source](https://github.com/tensorflow/federated/blob/v0.87.0\nVersion 2.0, January 2004\nLicensed under the Apache License, Version 2.0 (the) \n\n __ne__(\n other: object\n ) -\u003e bool\n\nReturn self!=value."]]