tldr

tdrgr: test driven red green refactor

28 February 2020 · tags: python - module

namedtuple module

namedtuple

Nice module to improve code readability

from collections import namedtuple

BuildTime = namedtuple("BT", "name id time")
build = BuildTime("", None, -1)

bt = ["XYZ", 123, "2019-02-04 12:13:01"]
try:
  build = BuildTime(*bt) # init expects exact number of parameters, expanded by *
except TypeError:
  print("Wrong number of fields")
print(build.name, build.time)
XYZ 2019-02-04 12:13:01

https://pymotw.com/2/collections/namedtuple.html

https://pymotw.com/