[Python] Singledispatch based on value instead of type

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I building SPA on Django and I have one huge function with many if statement for checking state name of my object field. Like this:

    if self.state == 'new':
    do some logic
    if self.state == 'archive':
    do some logic


    and so on. I reading nice book "Fluent python" now, and I mention about @singledispatch decorator, it looks so great, but it can overide function only with diferent type of parametres like str, int, etc.
    Question is, if there in python or Django way to separate logic like in my huge function with overided function like singledispatch do?

    Continue reading...

Compartilhe esta Página