[Python] How to check the length of each variable in a named tuple?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    Suppose I have a named tuple

    my_tuple = collections.namedtuple(
    "my_tuple",
    ["a", "b", "c", "d", "e"],
    )


    I want to check that the length of each value in an instance of my_tuple is equal to some constant value. Is there a way to automate that?

    I think I can use getattr here but I read that it's typically not good practice and violates encapsulation, though it seems reasonable in this case?

    Continue reading...

Compartilhe esta Página