4 ms·
One minor correction about named tuples. The syntax for field names can be an iterable or a string. The preferred way to do it is: > Person = namedtuple(
by raymondh 13y ago
One minor correction about named tuples. The syntax for field names can be an iterable or a string. The preferred way to do it is:
> Person = namedtuple('Person', ['name', 'age', 'gender'])
The string form is there to accomodate use cases where the field names are being cut-and-pasted from SQL or CSV headers or somesuch. Also, some people find it easier to type:
> Person = namedtuple('Person', 'name, age, gender')
- eliben 13y agoAll the same ways were chosen for the Enum functional API, by the way (list, dict, string separated with either spaces or commas). In general, the functional API was inspired mostly by namedtuple. It's not that the namedtuple syntax is pretty. It's not, we all know that, but that's what Python can offer at this point. But it's good enough so people learn to live with it, get used to it and intuitively understand what it means, and that's good enough. That said, the preferred and recommended approach to define enums is with the class syntax.