Skip to content

Commit ca41dc2

Browse files
Anton-2dpgeorge
authored andcommitted
py/objnamedtuple: Allow passing field names as a tuple.
So the documentation's example works. Besides, a tuple can be more memory efficient.
1 parent 2133924 commit ca41dc2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

py/objnamedtuple.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
165165
fields_in = mp_obj_str_split(1, &fields_in);
166166
}
167167
#endif
168-
if (!MP_OBJ_IS_TYPE(fields_in, &mp_type_list)) {
169-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "list required"));
170-
}
171-
mp_obj_list_get(fields_in, &n_fields, &fields);
168+
mp_obj_get_array(fields_in, &n_fields, &fields);
172169
return mp_obj_new_namedtuple_type(name, n_fields, fields);
173170
}
174171
MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);

tests/basics/namedtuple1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
t = T3(1, 2)
6767
print(t.foo, t.bar)
6868

69+
# Try tuple
70+
T4 = namedtuple("TupTuple", ("foo", "bar"))
71+
t = T4(1, 2)
72+
print(t.foo, t.bar)
73+
6974
# Try single string with comma field seperator
7075
# Not implemented so far
7176
#T2 = namedtuple("TupComma", "foo,bar")

0 commit comments

Comments
 (0)