summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/namedtuple1.py
blob: 9afeed9408357a9122006d390dd672c0701b5455 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
try:
    from collections import namedtuple
except ImportError:
    from _collections import namedtuple

T = namedtuple("Tup", ["foo", "bar"])
# CPython prints fully qualified name, what we don't bother to do so far
#print(T)
for t in T(1, 2), T(bar=1, foo=2):
    print(t)
    print(t[0], t[1])
    print(t.foo, t.bar)

    print(len(t))
    print(bool(t))
    print(t + t)
    print(t * 3)

    print([f for f in t])

    print(isinstance(t, tuple))

try:
    t[0] = 200
except TypeError:
    print("TypeError")
try:
    t.bar = 200
except AttributeError:
    print("AttributeError")

try:
    t = T(1)
except TypeError:
    print("TypeError")

try:
    t = T(1, 2, 3)
except TypeError:
    print("TypeError")

try:
    t = T(foo=1)
except TypeError:
    print("TypeError")

try:
    t = T(1, foo=1)
except TypeError:
    print("TypeError")

# Try single string
# Not implemented so far
#T3 = namedtuple("TupComma", "foo bar")
#t = T3(1, 2)

# Try single string with comma field seperator
# Not implemented so far
#T2 = namedtuple("TupComma", "foo,bar")
#t = T2(1, 2)