/tests/basics/Functions32.py

https://bitbucket.org/pombredanne/nuitka · Python · 143 lines · 80 code · 46 blank · 17 comment · 2 complexity · 3901146c2a2b1e8ee37cd181fa6c1593 MD5 · raw file

  1. # Copyright 2013, Kay Hayen, mailto:kay.hayen@gmail.com
  2. #
  3. # Python tests originally created or extracted from other peoples work. The
  4. # parts were too small to be protected.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. def kwonlysimple( *, a ):
  19. return a
  20. print( "Most simple case", kwonlysimple( a = 3 ) )
  21. def kwonlysimpledefaulted( *, a = 5 ):
  22. return a
  23. print( "Default simple case", kwonlysimpledefaulted() )
  24. def default1():
  25. print( "Called", default1 )
  26. return 1
  27. def default2():
  28. print( "Called", default2 )
  29. return 2
  30. def default3():
  31. print( "Called", default3 )
  32. return 3
  33. def default4():
  34. print( "Called", default4 )
  35. return 4
  36. def annotation1():
  37. print ( "Called", annotation1 )
  38. return "a1"
  39. def annotation2():
  40. print ( "Called", annotation2 )
  41. return "a2"
  42. def annotation3():
  43. print ( "Called", annotation3 )
  44. return "a3"
  45. def annotation4():
  46. print ( "Called", annotation4 )
  47. return "a4"
  48. def annotation5():
  49. print ( "Called", annotation5 )
  50. return "a5"
  51. def annotation6():
  52. print ( "Called", annotation6 )
  53. return "a6"
  54. def annotation7():
  55. print ( "Called", annotation7 )
  56. return "a7"
  57. def annotation8():
  58. print ( "Called", annotation8 )
  59. return "a8"
  60. def annotation9():
  61. print ( "Called", annotation9 )
  62. return "a9"
  63. def kwonlyfunc( x: annotation1(), y: annotation2() = default1(), z: annotation3() = default2(), *, a: annotation4(), b: annotation5() = default3(), c: annotation6() = default4(), d: annotation7(), **kw: annotation8() ) -> annotation9():
  64. print( x, y, z, a, b, c, d )
  65. print( kwonlyfunc.__kwdefaults__ )
  66. print( "Keyword only function" )
  67. kwonlyfunc( 7, a = 8, d = 12 )
  68. print( "Annotations come out as", kwonlyfunc.__annotations__ )
  69. kwonlyfunc.__annotations__ = {}
  70. print( "After updating to None it is", kwonlyfunc.__annotations__ )
  71. kwonlyfunc.__annotations__ = { "k" : 9 }
  72. print( "After updating to None it is", kwonlyfunc.__annotations__ )
  73. def kwonlystarfunc( *, a, b, **d ):
  74. return a, b, d
  75. print( "kwonlystarfunc", kwonlystarfunc( a = 8, b = 12, k = 9, j = 7 ) )
  76. def deeplyNestedNonLocalWrite():
  77. x = 0
  78. y = 0
  79. def f():
  80. def g():
  81. nonlocal x
  82. x = 3
  83. return x
  84. return g()
  85. return f(), x
  86. print( "Deeply nested non local writing function", deeplyNestedNonLocalWrite() )
  87. def deletingClosureVariables():
  88. try:
  89. x = 1
  90. def g():
  91. nonlocal x
  92. del x
  93. g()
  94. g()
  95. except Exception as e:
  96. return e
  97. print( "Using deleted non-local vaiables", deletingClosureVariables() )