forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamedtuple.py
More file actions
21 lines (18 loc) · 793 Bytes
/
Copy pathnamedtuple.py
File metadata and controls
21 lines (18 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
__author__ = 'ipetrash'
if __name__ == '__main__':
# TODO: https://docs.python.org/3/library/collections.html#collections.namedtuple_example
# Basic example
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22) # instantiate with positional or keyword arguments
print(p[0] + p[1]) # indexable like the plain tuple (11, 22)
x, y = p # unpack like a regular tuple
print(x, y)
print(p.x + p.y) # fields also accessible by name
print(p) # readable __repr__ with a name=value style
print()
XYZPoint = namedtuple('XYZPoint', ['x', 'y', 'z'])
xyz = XYZPoint(2, 2, z=5)
print(xyz)
print("z:", xyz.z)
print("Points: x: {x}, y: {y}, z: {z}".format(**xyz.__dict__))