py/objnamedtuple: Allow passing field names as a tuple.

So the documentation's example works.  Besides, a tuple can be more
memory efficient.
This commit is contained in:
Antonin ENFRUN 2016-05-22 19:28:04 +02:00 committed by Damien George
parent 2133924e46
commit ca41dc2750
2 changed files with 6 additions and 4 deletions

View File

@ -165,10 +165,7 @@ STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
fields_in = mp_obj_str_split(1, &fields_in);
}
#endif
if (!MP_OBJ_IS_TYPE(fields_in, &mp_type_list)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "list required"));
}
mp_obj_list_get(fields_in, &n_fields, &fields);
mp_obj_get_array(fields_in, &n_fields, &fields);
return mp_obj_new_namedtuple_type(name, n_fields, fields);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);

View File

@ -66,6 +66,11 @@ T3 = namedtuple("TupComma", "foo bar")
t = T3(1, 2)
print(t.foo, t.bar)
# Try tuple
T4 = namedtuple("TupTuple", ("foo", "bar"))
t = T4(1, 2)
print(t.foo, t.bar)
# Try single string with comma field seperator
# Not implemented so far
#T2 = namedtuple("TupComma", "foo,bar")