Besonderhede van voorbeeld: 5510655007861788645

Metadata

Author: WikiMatrix

Data

English[en]
The following example binds the variable "threshold" in an anonymous function that compares the input to the threshold. def comp(threshold): return lambda x: x < threshold This can be used as a sort of generator of comparison functions: >>> func_a = comp(10) >>> func_b = comp(20) >>> print(func_a(5), func_a(8), func_a(13), func_a(21)) True True False False >>> print(func_b(5), func_b(8), func_b(13), func_b(21)) True True True False It would be impractical to create a function for every possible comparison function and may be too inconvenient to keep the threshold around for further use.
Spanish[es]
El siguiente ejemplo vincula la variable "threshold" (umbral) en una función anónima que compara la entrada con el umbral. def comp(threshold): return lambda x: x < threshold Esto se puede utilizar como una especie de generador de funciones de comparación: >>> func_a = comp(10) >>> func_b = comp(20) >>> print func_a(5), func_a(8), func_a(13), func_a(21) True True False False >>> print func_b(5), func_b(8), func_b(13), func_b(21) True True True False Sería poco práctico crear una función para cada función de comparación posible y puede ser demasiado inconveniente para mantener el umbral alrededor para su uso posterior.

History

Your action: