[Python] Why is this use of Python's nonlocal not recognizing the variable defined in the...

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I've two UDFs that are defined separately (not nested), and they cannot be defined while nested.

    UDF #1

    def varied_test(data, alg):
    fit_data = []
    alg(2, 4, 6)
    return(fit_data)


    UDF #2

    def alg_1(param_1, param_2, param_3):
    nonlocal fit_data
    y = ((param_1*3) - param_2)/param_3
    fit_data.append(y)


    The driver script calls UDF #1 with UDF #2 as a parameter:

    tester = varied_test(df, alg_1)


    When trying to run, it throws SyntaxError:

    SyntaxError: no binding for nonlocal 'fit_data' found


    Do my UDFs really need to be defined while nested to use nonlocal, or is there another way that 'fit_data' can be recognized?

    Continue reading...

Compartilhe esta Página