48

I have a module with collection:

import collections
named_tuple_sex = collections.namedtuple(
                    'FlightsResultsSorter',
                        ['TotalPriceASC',
                         'TransfersASC',
                         'FlightTimeASC',
                         'DepartureTimeASC',
                         'DepartureTimeDESC',
                         'ArrivalTimeASC',
                         'ArrivalTimeDESC',
                         'Airlines']
                    )
FlightsResultsSorter = named_tuple_sex(
    FlightsResultsSorter('TotalPrice', SortOrder.ASC),
    FlightsResultsSorter('Transfers', SortOrder.ASC),
    FlightsResultsSorter('FlightTime', SortOrder.ASC),
    FlightsResultsSorter('DepartureTime', SortOrder.ASC),
    FlightsResultsSorter('DepartureTime', SortOrder.DESC),
    FlightsResultsSorter('ArrivalTime', SortOrder.ASC),
    FlightsResultsSorter('ArrivalTime', SortOrder.DESC),
    FlightsResultsSorter('Airlines', SortOrder.ASC)
)

and in another module, I iterate by this collection and I want to get the name of the item:

for x in FlightsResultsSorter:
            self.sort(x)

so in the code above, I want instead of x (which is an object) to pass, for example, DepartureTimeASC or ArrivalTimeASC.

How can I get this name?

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
user278618
  • 19,306
  • 42
  • 126
  • 196

2 Answers2

88

If you're trying to get the actual names, use the _fields attribute:

from collections import namedtuple

Point = namedtuple('Point', 'x, y')
p = Point(x=1, y=2)
for name in Point._fields:
    print(name, getattr(p, name))
wim
  • 338,267
  • 99
  • 616
  • 750
ars
  • 120,335
  • 23
  • 147
  • 134
  • 4
    Is it recommended to access protected members/functions (ex _fields, _asdict()) of a class? – awwwd May 23 '21 at 14:27
  • For python 3+, use `print(f"{name}: {getattr(p, name)}")` – cowlinator Mar 19 '22 at 00:58
  • @awwwd These are not protected members, they are documented ([`_fields`](https://docs.python.org/3/library/collections.html#collections.somenamedtuple._fields), [`_asdict`](https://docs.python.org/3/library/collections.html#collections.somenamedtuple._asdict)). – wim Apr 12 '23 at 03:17
38
from itertools import izip

for x, field in izip(FlightsResultsSorter, named_tuple_sex._fields):
    print x, field

You can also use FlightsResultsSorter._asdict() to get a dict.

jd.
  • 10,678
  • 3
  • 46
  • 55
  • 6
    Is there any other way to do this without accessing a private member of the namedtuple? – user3885927 Apr 20 '15 at 18:16
  • 10
    these aren't considered private members -- the _ convention is only to avoid collisions with, say, a field called _fields. – Michael Scott Asato Cuthbert Oct 26 '16 at 20:56
  • 6
    That's not true. Prepending (_field) means it's private. Appending (fields_) is what is used to avoid collisions. – Younes El Ouarti Mar 16 '20 at 10:00
  • 5
    @YounesEO They're publicly documented and the doc has these forewords *To prevent conflicts with field names, the method and attribute names start with an underscore.* You could say that they're not ideally named and don't abide by the convention, but they're definitely not private. – Michael Ekoka Oct 29 '20 at 00:21