Mutable NamedTuple in python
Question:
I’m looking for a good struct pattern in python. The NamedTuple
class constructor is almost what I want. Consider the following:
class MyStruct(NamedTuple):
prop1: str
prop2: str
prop3: Optional[str] = "prop3" # should allow default values
prop4: Optional[str] = "prop4"
...
# allow this
struct = MyStruct(prop1="prop1", prop2="prop2")
# allow this (order agnostic)
struct = MyStruct(prop2="prop2", prop1="prop1")
# allow this (supply value to optional properties)
struct = MyStruct(prop1="prop1", prop2="prop2", prop3="hello")
# allow this (access via . syntax)
print(struct.prop3)
# allow this (mutate properties)
struct.prop2 = "hello!"
# don't allow this (missing required properties, prop1 and prop2)
struct = MyStruct()
NamedTuple
does everything except the mutability piece. I’m looking for something similar to a Namespace
with type hints. I looked at TypedDict
as well, but don’t like that you can’t access properties with struct.prop1
, but instead must use struct["prop1"]
. I also need to be able to supply default values when the struct is instantiated.
I thought about just a regular class as well, but my object may have many properties (20-30), and the order in which properties are supplied to the constructor should not matter.
I also want type hinting in my editor.
What’s the best way to achieve this?
Answers:
You can use @dataclass
:
from dataclasses import dataclass
from typing import Optional
@dataclass
class MyStruct:
prop1: str
prop2: str
prop3: Optional[str] = "prop3"
prop4: Optional[str] = "prop4"
If dataclass doesn’t satisfy your requirements, try recordclass, literally a mutable named tuple.
I’m looking for a good struct pattern in python. The NamedTuple
class constructor is almost what I want. Consider the following:
class MyStruct(NamedTuple):
prop1: str
prop2: str
prop3: Optional[str] = "prop3" # should allow default values
prop4: Optional[str] = "prop4"
...
# allow this
struct = MyStruct(prop1="prop1", prop2="prop2")
# allow this (order agnostic)
struct = MyStruct(prop2="prop2", prop1="prop1")
# allow this (supply value to optional properties)
struct = MyStruct(prop1="prop1", prop2="prop2", prop3="hello")
# allow this (access via . syntax)
print(struct.prop3)
# allow this (mutate properties)
struct.prop2 = "hello!"
# don't allow this (missing required properties, prop1 and prop2)
struct = MyStruct()
NamedTuple
does everything except the mutability piece. I’m looking for something similar to a Namespace
with type hints. I looked at TypedDict
as well, but don’t like that you can’t access properties with struct.prop1
, but instead must use struct["prop1"]
. I also need to be able to supply default values when the struct is instantiated.
I thought about just a regular class as well, but my object may have many properties (20-30), and the order in which properties are supplied to the constructor should not matter.
I also want type hinting in my editor.
What’s the best way to achieve this?
You can use @dataclass
:
from dataclasses import dataclass
from typing import Optional
@dataclass
class MyStruct:
prop1: str
prop2: str
prop3: Optional[str] = "prop3"
prop4: Optional[str] = "prop4"
If dataclass doesn’t satisfy your requirements, try recordclass, literally a mutable named tuple.