Python arglist examples


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 3:22 p.m.

FORMAT: Python

SIZE: 2.0 kB

HITS: 1147

  1. >>> def exampleArgs ( * argList ):
  2. ... return argList
  3. ...
  4. >>> exampleArgs (1, 2, 3)
  5. (1, 2, 3)
  6. >>> def exampleArgs ( ** argList ):
  7. ... return argList
  8. ...
  9. >>> exampleArgs (1, 2, 3)
  10. Traceback (most recent call last):
  11. File "<stdin>", line 1, in <module>
  12. TypeError: exampleArgs() takes exactly 0 arguments (3 given)
  13. >>> exampleArgs (a = 1, b = 2)
  14. {'a': 1, 'b': 2}
  15. >>> def exampleArgs (a, b, * argTuple, ** argDict ):
  16. ... yield a
  17. ... yield b
  18. ... yield argTuple
  19. ... yield argDict
  20. ...
  21. >>> for returnValue in exampleArgs( "a", "b", 1, 2, 3, keyOne=1, keyTwo=2):
  22. ... print returnValue
  23. ...
  24. a
  25. b
  26. (1, 2, 3)
  27. {'keyOne': 1, 'keyTwo': 2}
  28. >>> for returnValue in exampleArgs( "a", "b", 1, 2, 3, keyOne=1, keyTwo=2):
  29. ... print returnValue
  30. ...
  31. a
  32. b
  33. (1, 2, 3)
  34. {'keyOne': 1, 'keyTwo': 2}
  35. >>> for returnValue in exampleArgs( "a", "b", 1, 2, 3, keyOne=1, keyOne=2):
  36. ... print returnValue
  37. ...
  38. File "<stdin>", line 1
  39. SyntaxError: keyword argument repeated
  40. >>> def exampleArgs (a=1, b=2, * argTuple, ** argDict ):
  41. ... yield a
  42. ... yield b
  43. ... yield argTuple
  44. ... yield argDict
  45. ...
  46. >>> for returnValue in exampleArgs():
  47. ... print returnValue
  48. ...
  49. 1
  50. 2
  51. ()
  52. {}
  53. >>> for returnValue in exampleArgs(c = 3):
  54. ... print returnValue
  55. ...
  56. 1
  57. 2
  58. ()
  59. {'c': 3}
  60. >>> for returnValue in exampleArgs(3):
  61. ... print returnValue
  62. ...
  63. 3
  64. 2
  65. ()
  66. {}
  67. >>> for returnValue in exampleArgs(a=1, b=2, 3):
  68. ... print returnValue
  69. ...
  70. File "<stdin>", line 1
  71. SyntaxError: non-keyword arg after keyword arg
  72. >>> for returnValue in exampleArgs(1, 2, 3):
  73. ... print returnValue
  74. ...
  75. 1
  76. 2
  77. (3,)
  78. {}
  79. >>> for returnValue in exampleArgs(3,):
  80. ... print returnValue
  81. ...
  82. 3
  83. 2
  84. ()
  85. {}
  86. >>> for returnValue in exampleArgs((3,)):
  87. ... print returnValue
  88. ...
  89. (3,)
  90. 2
  91. ()
  92. {}

comments powered by Disqus