MCPcopy Create free account
hub / github.com/EntilZha/PyFunctional / is_namedtuple

Function is_namedtuple

functional/util.py:41–53  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

39
40
41def 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
56def identity(arg):

Callers 5

_insert_itemMethod · 0.90
tabulateMethod · 0.90
_wrapFunction · 0.90
test_is_namedtupleMethod · 0.90
is_tabulatableFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_is_namedtupleMethod · 0.72