View source on GitHub |
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
@classmethod
named( **kwargs ) -> 'Struct'
Constructs a new Struct
with all named elements.
unnamed
@classmethod
unnamed( *args ) -> 'Struct'
Constructs a new Struct
with all unnamed elements.
__eq__
__eq__(
other: object
) -> bool
Return self==value.
__getitem__
__getitem__(
key: Union[int, str, slice]
) -> _T
__iter__
__iter__() -> Iterator[_T]
__len__
__len__() -> int
__ne__
__ne__(
other: object
) -> bool
Return self!=value.