Advertisement
teslariu

func long variable

Jan 12th, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # cantidad de argumentos variables
  5. """
  6.  
  7. # Tendrìa que hacer algo asì:
  8. def sumar(numeros):
  9.    return sum(numeros)
  10.    
  11.    
  12. print(sumar([1,2,3,4,5]))
  13.  
  14. # Casos correctos
  15. # 1)
  16. def sumar2(*args):
  17.    print(type(args)) # muestra el tipo de dato de args
  18.    return sum(args)
  19.    
  20. print(sumar2(1,2))
  21. print(sumar2(1,2,3,4,5))
  22.  
  23. # 2)
  24. def sumar3(**kwargs):
  25.    print(type(kwargs)) # muestra el tipo de dato de args
  26.    # kwargs = {'a':1, 'b':10, 'c':3}
  27.    valores = list(kwargs.values())
  28.    return sum(valores)
  29.    
  30. print(sumar3(a=1, b=10, c=3))
  31. """
  32. # forma general para pasaje de argumentos
  33. # orden de argumentos
  34. # 1º posicionales sin valor x defecto (a,b,c)
  35. # 2º *args (cantidad arbitraria de argumentos)
  36. # 3º posicionales con valor x defecto (d=10, e=12)
  37. # 4º keyword arguments (**kwargs)
  38.  
  39. def funcion(a, b, c, *args, d=10, e=12, **kwargs):
  40.     print(a)
  41.     print(b)
  42.     print(c)
  43.     print(args)
  44.     print(d)
  45.     print(e)
  46.     print(kwargs)
  47.    
  48. funcion(1,2,3,100,200,300,k=20,z=25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement