Add basic collections.namedtuple implementation.

This commit is contained in:
Paul Sokolovsky
2014-02-27 22:22:04 +02:00
parent 61f9b1c621
commit d08fd68664
6 changed files with 192 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
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)
t = T(1, 2)
print(t)
print(t.foo, t.bar)
print(isinstance(t, tuple))
try:
t[0] = 200
except TypeError:
print("TypeError")
try:
t.bar = 200
except AttributeError:
print("AttribiteError")
try:
t = T(1)
except TypeError:
print("TypeError")
try:
t = T(1, 2, 3)
except TypeError:
print("TypeError")
# Try comma field separator
T2 = namedtuple("TupComma", "foo,bar")
t = T2(1, 2)
print(t)
print(t.foo, t.bar)
# Try list of fields
# Not implemented so far
#T3 = namedtuple("TupComma", ["foo", "bar"])
#t = T3(1, 2)