[Python] How can I make I make lambdas[0](10) equal to 19?

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    Simple array of lambda functions that is used to calculate the addition of a number on the fly.

    def lambda_array():
    # initialize an empty array
    lambda_methods = []
    # implement a for loop to count from 0 to 9
    for i in range(10):
    # append the lambda function to the array defined above
    lambda_methods.append(lambda x, i=i: x + i)

    return lambda_methods

    print(lambdas[0](10))


    It prints 10, instead of 19

    Continue reading...

Compartilhe esta Página