Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
PEP8 the last added usage example
Source Link
gz.

Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4.

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or youYou can also use named arguments:

m = MyStruct(field1 = "foo"field1="foo", field2 = "bar"field2="bar", field3 = "baz"field3="baz")

Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4.

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")

Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4.

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

You can also use named arguments:

m = MyStruct(field1="foo", field2="bar", field3="baz")
This was added to the standard library nearly 6 years ago - that should be mentioned from the get-go, not as a footnote at the bottom.
Source Link
ArtOfWarfare

ForUse a "whole" solutionnamed tuple, seewhich was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4. 

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")

Named tuple is part of the collections module, which was added to the standard library in Python 2.6.

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")

Named tuple is part of the collections module, which was added to the standard library in Python 2.6.

Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4. 

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")
Added example.
Source Link
dan-gph

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")

Named tuple is part of the collections module in, which was added to the standard library sincein Python 2.6.

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.

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")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

Or you can use named arguments:

m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")

Named tuple is part of the collections module, which was added to the standard library in Python 2.6.

added link to documentation
Source Link
Harriv
Loading
Also in the up-coming Python 2.6
Source Link
gz.
Loading
Source Link
gz.
Loading
lang-py