Contenido del curso
Calidad y Profesionalismo del Código
Manejo de Datos y Recursos
Optimización y Pruebas
Creación de Aplicaciones de Consola
- 19

UV: Faster Python Dependency Management
07:17 min - 20

How Python Modules Keep Your Code Organized
07:14 min - 21

Organizing Python Code Into Packages
09:06 min - 22

Función enumerate en Python para indexar listas automáticamente
09:31 min - 23

Filtrado de listas con filter en Python
11:44 min - 24

Función map para calcular tiempo de lectura en Python
08:59 min - 25

Conexión de OpenAI API con variables de entorno en Python
11:17 min
Python *args: Variable Function Arguments
Resumen
Python lets you pass a variable number of arguments to a function using *args, and that single asterisk changes how flexible your code can be. You will learn how dynamic arguments work, why their order matters, and how to apply them when building API clients in real projects.
What does
*argsmean in Python? It is a syntax that lets a function receive an unlimited number of positional arguments, grouped automatically into a tuple you can iterate over.
Why do you need dynamic arguments in Python?
When you build a function, you usually know exactly which parameters it will receive. But real applications, like one that pulls news from multiple providers, rarely behave that way.
Imagine you are coding a news aggregator. You want to connect to News API and also to The Guardian API. Each one expects a different set of parameters:
- News API needs
api_key,query,timeout, andretries. - Guardian API needs
api_key,section,from_date,timeout, andretries. - Both share some fields, but not all of them.
If you hardcode every parameter for every client, your code becomes rigid. That is exactly the problem *args solves.
How do you define a function with *args?
You add an asterisk before the parameter name. By convention, that name is args, although you can call it whatever you want.
python def ejemplo_args(*args): print(f"args: {args}") print(f"type: {type(args)}")
Now you can call the function with any number of values:
python ejemplo_args(1, 2, 3) ejemplo_args("Hola", "mundo") ejemplo_args()
When you run python main.py, you will see that args prints as a tuple. That is key: Python wraps every extra positional argument into a tuple, an immutable structure you can read and iterate but not modify.
How do you mix required parameters with *args?
Dynamic arguments are powerful, but most functions still need at least one mandatory value. In a news client, for example, the api_key is non negotiable.
The trick is order: required parameters go first, then *args collects the rest.
python def ejemplo_args(api_key, *args): print(f"api_key: {api_key}") print(f"args: {args}") print(f"type: {type(args)}")
If you call ejemplo_args("my_key", "Hola", "mundo"), Python assigns "my_key" to api_key and bundles "Hola" and "mundo" inside the tuple args.
*Why does the order of parameters matter with args? Because
*argscaptures every positional argument after the named ones. If you place a required parameter after*args, Python cannot tell where the dynamic list ends.
What happens if you forget a required argument?
Python will raise an error like missing 1 required positional argument: 'api_key'. This is helpful: it forces you to be explicit about what your function truly needs to run.
If instead you call the function without passing the key, the interpreter stops execution and points exactly to the missing field.
How do you apply *args to real API clients?
Let us bring it back to the news app. You can define two functions that simulate the connection to each provider:
python def news_api_client(api_key, query, timeout=30, retries=3): return f"News API query: {query} timeout: {timeout}"
def guardian_client(api_key, section, from_date, timeout=30, retries=3): return f"Guardian section: {section} desde: {from_date} timeout: {timeout}"
Both functions share api_key, timeout, and retries, but each one demands its own specific fields. With *args, you could refactor a wrapper function that forwards only the parameters each client needs, without rewriting the signature every time you add a provider.
This pattern is common when you build SDKs, plugin systems, or any function that must adapt to different inputs at runtime.
When should you choose *args over named parameters?
Use *args when:
- The number of inputs changes between calls.
- You want a flexible interface without listing every option.
- You are passing values down to another function that knows what to do with them.
Stick with named parameters when each value has a clear, fixed role. Clarity always beats flexibility if your function only needs three known fields.
*Can you modify the values inside args? No, because
*argsis a tuple and tuples are immutable. You can read them, loop through them, or convert them to a list if you need to change them.
What is your turn to practice?
Build a function that receives any amount of numbers through *args and returns the total sum. Test it with two numbers, with ten, and with none, and check how Python behaves in each scenario. Drop your version in the comments and compare it with what others wrote.