2014-04-13 08:17:04 +01:00
|
|
|
try:
|
|
|
|
from collections import namedtuple
|
|
|
|
except ImportError:
|
|
|
|
from _collections import namedtuple
|
2014-02-27 20:22:04 +00:00
|
|
|
|
2014-12-20 15:37:40 +00:00
|
|
|
T = namedtuple("Tup", ["foo", "bar"])
|
2014-02-27 20:22:04 +00:00
|
|
|
# CPython prints fully qualified name, what we don't bother to do so far
|
|
|
|
#print(T)
|
2014-12-20 15:38:40 +00:00
|
|
|
for t in T(1, 2), T(bar=1, foo=2):
|
|
|
|
print(t)
|
|
|
|
print(t[0], t[1])
|
|
|
|
print(t.foo, t.bar)
|
2014-02-27 20:22:04 +00:00
|
|
|
|
2014-12-20 15:38:40 +00:00
|
|
|
print(len(t))
|
|
|
|
print(bool(t))
|
|
|
|
print(t + t)
|
|
|
|
print(t * 3)
|
2014-02-27 20:49:47 +00:00
|
|
|
|
2014-12-20 15:38:40 +00:00
|
|
|
print([f for f in t])
|
2014-05-10 18:15:49 +01:00
|
|
|
|
2014-12-20 15:38:40 +00:00
|
|
|
print(isinstance(t, tuple))
|
2014-02-27 20:22:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
t[0] = 200
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
try:
|
|
|
|
t.bar = 200
|
|
|
|
except AttributeError:
|
2014-12-20 15:37:40 +00:00
|
|
|
print("AttributeError")
|
2014-02-27 20:22:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1, 2, 3)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2014-12-20 15:38:40 +00:00
|
|
|
try:
|
|
|
|
t = T(foo=1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1, foo=1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2014-12-20 15:37:40 +00:00
|
|
|
# Try single string
|
2014-02-27 20:22:04 +00:00
|
|
|
# Not implemented so far
|
2014-12-20 15:37:40 +00:00
|
|
|
#T3 = namedtuple("TupComma", "foo bar")
|
2014-02-27 20:22:04 +00:00
|
|
|
#t = T3(1, 2)
|
2014-12-20 15:37:40 +00:00
|
|
|
|
|
|
|
# Try single string with comma field seperator
|
|
|
|
# Not implemented so far
|
|
|
|
#T2 = namedtuple("TupComma", "foo,bar")
|
|
|
|
#t = T2(1, 2)
|