Funciones avanzadas
Cuando ni tú sabes cuántos argumentos vas a necesitar, y funciones tan cortas que ni piden nombre.
Advanced functions
When even you do not know how many arguments you will need, and functions so short they skip the name.
Hasta ahora tus funciones reciben una cantidad fija de parámetros. Pero, ¿y si quieres sumar
2 números, o 5, o 20, con la misma función? Para eso está *args: junta todos los
argumentos sueltos en una tupla.
So far your functions take a fixed number of parameters. But what if you want to add 2
numbers, or 5, or 20, with the same function? That is what *args is for: it packs
every loose argument into a tuple.
Con dos asteriscos pasa lo mismo, pero con argumentos que llegan con nombre. Python los junta en un diccionario.
With two asterisks the same thing happens, but with arguments that arrive named. Python packs them into a dictionary.
Se pueden combinar: parámetros normales, luego
*args, y al final **kwargs. Ese orden no es opcional, Python lo
exige así.
They can combine: normal parameters, then *args, and
finally **kwargs. That order is not optional, Python requires it.
Una lambda es una función chica, sin nombre, escrita en una sola línea.
Sirve para esos casos donde definir una función completa con def se siente
exagerado.
A lambda is a small, nameless function written on a single line. It is for
cases where writing a whole def function feels like overkill.
map() aplica una función a cada elemento de una lista. filter()
se queda solo con los elementos que cumplen una condición. A las dos se les pasa una función
como argumento — casi siempre una lambda.
map() applies a function to every item in a list. filter() keeps
only the items that meet a condition. Both take a function as an argument — almost always a
lambda.
En Python, la comprensión de listas suele preferirse a
map/filter porque se lee más de corrido. Pero vas a encontrar ambas
formas en código real, así que conviene reconocer las dos.
In Python, list comprehensions are usually preferred over
map/filter because they read more fluently. But you will find both
forms in real code, so it pays to recognise them both.
*args y **kwargs para funciones flexibles, lambda
para funciones chicas y desechables, y map/filter para procesar
listas sin escribir el bucle a mano.
Ahora que sabes empaquetar datos, toca aprender a intercambiarlos: el formato que habla todo internet.
*args and **kwargs for flexible functions, lambda
for small, throwaway functions, and map/filter for processing lists
without writing the loop by hand.
Now that you can pack data, it is time to learn to exchange it: the format the whole internet speaks.