{ "cells": [ { "cell_type": "markdown", "id": "729dc9bd-b5b9-4a10-8d6a-9c93bb1c6fd2", "metadata": { "tags": [] }, "source": [ "Argumentos posicionales, valores por defecto y número variable de argumentos\n", "===\n", "\n", "* Duración de la lección: 4:37 min" ] }, { "cell_type": "code", "execution_count": 1, "id": "cba98591-6d30-44bc-9a1b-d70ef4cd469a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " first_arg: 1\n", "second_arg: 2\n", " args: ()\n", " kwargs: {}\n" ] } ], "source": [ "#\n", "# Tipos de argumentos\n", "#\n", "def my_function(first_arg, second_arg=2, *args, **kwargs):\n", " print(\" first_arg:\", first_arg)\n", " print(\"second_arg:\", second_arg)\n", " print(\" args:\", args)\n", " print(\" kwargs:\", kwargs)\n", "\n", "\n", "#\n", "# Llamada con argumentos posicionales\n", "#\n", "my_function(1, 2)" ] }, { "cell_type": "code", "execution_count": 2, "id": "b3266295-510d-47e5-8236-67d613910ea3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " first_arg: 1\n", "second_arg: 2\n", " args: ()\n", " kwargs: {}\n" ] } ], "source": [ "#\n", "# Llamada con argumentos por nombre\n", "#\n", "my_function(second_arg=2, first_arg=1)" ] }, { "cell_type": "code", "execution_count": 3, "id": "019253cd-dbc4-4efc-8e3b-39d30f699c1d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " first_arg: 1\n", "second_arg: 2\n", " args: (3, 4)\n", " kwargs: {}\n" ] } ], "source": [ "#\n", "# Llamada con argumentos extra sin nombre\n", "#\n", "my_function(1, 2, 3, 4)" ] }, { "cell_type": "code", "execution_count": 4, "id": "bef827df-93ce-4073-b734-fa6d91a8a7ca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " first_arg: 1\n", "second_arg: 2\n", " args: ()\n", " kwargs: {'third_arg': 3}\n" ] } ], "source": [ "#\n", "# Llamada con argumentos extra con nombre\n", "#\n", "my_function(first_arg=1, second_arg=2, third_arg=3)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5b4194a4-33c9-4fcf-b098-f239e41fd9ff", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " first_arg: 1\n", "second_arg: 2\n", " args: (3, 4)\n", " kwargs: {'a': 5, 'b': 6, 'c': 7}\n" ] } ], "source": [ "#\n", "# Combinacion de los casos anteriores\n", "#\n", "my_function(1, 2, 3, 4, a=5, b=6, c=7)" ] }, { "cell_type": "code", "execution_count": 6, "id": "855a97b7-ab03-41df-a1a9-451f4808455d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def my_function(a, b, c):\n", " return a + b + c\n", "\n", "\n", "args = {\n", " \"a\": 1,\n", " \"b\": 2,\n", " \"c\": 3,\n", "}\n", "\n", "my_function(**args)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.7 ('.venv': venv)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" }, "vscode": { "interpreter": { "hash": "99683f87c1354598af39114791cc78b29c5617e49c4bfa4c5fb7492534eeeb87" } } }, "nbformat": 4, "nbformat_minor": 5 }