Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
3 of 6
added link to documentation
Harriv

For a "whole" solution, see Raymond Hettinger's named tuple recipe. It's nice for your basic example, but also covers a bunch of edge cases you might run into later as well. Your fragment above would be written as:

from collections import namedtuple
MyStruct = namedtuple("MyStruct", "field1 field2 field3")

Named tuple is part of collections module in standard library since Python 2.6.

gz