Last active
August 29, 2015 14:01
-
-
Save dmiro/df8e0c240c8c1087ca6a to your computer and use it in GitHub Desktop.
Python *args and **kwargs in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| *args and **kwargs in Python (variable length argument lists in Python) | |
| Especificar que una función puede ser llamada con un número arbitrario de argumentos. | |
| Estos argumentos serán organizados en una lista o diccionario. Antes del número | |
| variable de argumentos, cero o más argumentos normales pueden estar presentes | |
| http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/ | |
| """ | |
| def mifunc(vargs, vkwargs, *args, **kwargs): | |
| print "vargs:", vargs | |
| print "vkwargs:", vkwargs | |
| print "*args:", args | |
| print "**kwargs:", kwargs | |
| def mifunc2(name, lastname, age=30): | |
| print "name:", name | |
| print "lastname:", lastname | |
| print "age:", age | |
| def mifunc3(name): | |
| print "name:", name | |
| print range(3, 6) | |
| args = [3, 6] | |
| print range(*args) | |
| args = ["Marc","Martinez"] | |
| kwargs = {"name":"David", "lastname":"Miro"} | |
| print "args", args | |
| print "kwargs:", kwargs | |
| mifunc(args, kwargs, *args, **kwargs) | |
| mifunc(args, kwargs, *kwargs) | |
| mifunc2(*args) | |
| mifunc2(**kwargs) | |
| #result | |
| """ | |
| [3, 4, 5] | |
| [3, 4, 5] | |
| args ['Marc', 'Martinez'] | |
| kwargs: {'lastname': 'Miro', 'name': 'David'} | |
| vargs: ['Marc', 'Martinez'] | |
| vkwargs: {'lastname': 'Miro', 'name': 'David'} | |
| *args: ('Marc', 'Martinez') | |
| **kwargs: {'lastname': 'Miro', 'name': 'David'} | |
| vargs: ['Marc', 'Martinez'] | |
| vkwargs: {'lastname': 'Miro', 'name': 'David'} | |
| *args: ('lastname', 'name') | |
| **kwargs: {} | |
| name: Marc | |
| lastname: Martinez | |
| age: 30 | |
| name: David | |
| lastname: Miro | |
| age: 30 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment