Use Duck Typing to check if val is a named tuple. Checks that val is of type tuple and contains the attribute _fields which is defined for named tuples. :param val: value to check type of :return: True if val is a namedtuple
(val)
| 39 | |
| 40 | |
| 41 | def is_namedtuple(val): |
| 42 | """ |
| 43 | Use Duck Typing to check if val is a named tuple. Checks that val is of type tuple and contains |
| 44 | the attribute _fields which is defined for named tuples. |
| 45 | :param val: value to check type of |
| 46 | :return: True if val is a namedtuple |
| 47 | """ |
| 48 | val_type = type(val) |
| 49 | bases = val_type.__bases__ |
| 50 | if len(bases) != 1 or bases[0] != tuple: |
| 51 | return False |
| 52 | fields = getattr(val_type, "_fields", None) |
| 53 | return all(isinstance(n, str) for n in fields) |
| 54 | |
| 55 | |
| 56 | def identity(arg): |
no outgoing calls