[Python] Python UnboundLocalError even though the variable is in the same function and defined...

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

  1. Stack

    Stack Membro Participativo

    I have this code on my Flask app, few of them are these lines.

    @app.post("/dashboard/model/train/process")
    @login_required
    def model_train_process():
    #Really a lot of code...
    possible_parameter : ["categorical", "int", "float", "boolean"] # type: ignore
    for param in possible_parameter:
    #I'm doing something inside here...


    Just to be clear, I'm using # type: ignore because Pylance throw a warning because I'm using data type as a string value.

    Back to the main topic, I don't know why but Python throw this error


    UnboundLocalError: cannot access local variable 'possible_parameter' where it is not associated with a value
    ​

    That variable already defined before the loop, they are both in the same function, so why this error happened?

    Continue reading...

Compartilhe esta Página