Skip to content

Instantly share code, notes, and snippets.

@Everfighting
Created May 17, 2017 01:17
Show Gist options
  • Save Everfighting/80335befc3e7e301c83df5410f30b31a to your computer and use it in GitHub Desktop.
Save Everfighting/80335befc3e7e301c83df5410f30b31a to your computer and use it in GitHub Desktop.
>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> p # readable __repr__ with a name=value style
Point(x=11, y=22)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment