Skip to main content

Pythonic, C-style structs.

Project description

Pythonic, C-Style structs.

Structs are similar to namedtuples, but they allow type assertion and are defined the same way as any class. Creating a struct is easy.

class MyStruct(Struct):
    # fieldName = type
    field_0 = int
    field_1 = str

    # you can also allow multiple types
    multi_type = [int, str]  # fieldName = [type0, type1, ...]

    # and default values!
    with_default = int, 10  # fieldName = type, default

    with_multi_and_default = [int, str], "hi"  # fieldName = [type0, type1, ...], default

Once a struct is created, it’s fields can be changed, but they must match the given type or a TypeError will be raised.

del struct.field resets the field to None.

It’s also possible to nest structs. Any structs that are nested will automatically be initialized.

class OnlyInt(Struct):
    field_0 = int
    field_1 = int


class Nested(Struct):
    abc = str
    nest = OnlyInt  # nested struct
>>> print(Nested())
# Nested {
#     abc = None
#     nest = OnlyInt {
#         field_0 = None
#         field_1 = None
#     }
# }

>>> print(Nested(nest=None))
# Nested {
#     abc = None
#     nest = None
# }
  • Struct.dict() : dict with struct’s fields. {name: value, …}

  • Struct.fields : list of fields struct has.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Struction-0.0.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page