fork download
  1. import time
  2.  
  3.  
  4. """我的迭代器"""
  5. class Myiterator:
  6. def __init__(self, array: list, reverse: bool):
  7. self.array = array
  8. self.reverse = reverse
  9.  
  10. def __iter__(self):
  11. return self
  12.  
  13. def __next__(self):
  14. if len(self.array) == 0 and not self.array:
  15. raise StopIteration
  16. else:
  17. if self.reverse:
  18. return self.array.pop(0)
  19. else:
  20. return self.array.pop()
  21.  
  22.  
  23. myiterator = Myiterator([i for i in range(1, 41)], reverse=False)
  24. print("\033[32m%s开始反向循环%s\033[0m" % ("-"*10, "-"*10))
  25.  
  26. ends = []
  27. for i in myiterator:
  28. ends.append(i)
  29. # 判断列表长度并输出对应片段
  30. if len(ends) == 10:
  31. print(f"{ends[:10]}")
  32. elif len(ends) == 20:
  33. print(f"{ends[10:20]}")
  34. elif len(ends) == 30:
  35. print(f"{ends[20:30]}")
  36. elif len(ends) == 40:
  37. print(f"{ends[30:41]}")
  38. time.sleep(0.1)
  39. print(f"\n\033[32m反向循环结果:{ends}\033[0m")
  40.  
  41. print("\033[32m反向循环结束!\033[0m\n")
  42. print("\033[32m%s开始正向循环%s\033[0m" % ("-"*10, "-"*10))
  43.  
  44. starts = []
  45. for j in Myiterator([i for i in range(1, 41)], reverse=True):
  46. starts.append(j)
  47. if len(starts) == 10:
  48. print(f"{starts[:10]}")
  49. elif len(starts) == 20:
  50. print(f"{starts[10:20]}")
  51. elif len(starts) == 30:
  52. print(f"{starts[20:30]}")
  53. elif len(starts) == 40:
  54. print(f"{starts[30:41]}")
  55. time.sleep(0.1)
  56. print(f"\n\033[32m正向循环结果:{starts}\033[0m")
  57.  
  58. print("\033[32m正向循环结束!\033[0m")
  59.  
  60. try:
  61. results = []
  62. # 这里存在变量作用域问题,修改后使用双循环计算
  63. for x in range(len(starts)):
  64. start = starts[x]
  65. for c in range(len(ends)):
  66. if x == c: # 保证索引对应
  67. end = ends[c]
  68. result = start + end
  69. results.append(result)
  70. print(f"\n\033[32m计算结果:{results}\033[0m")
  71. results.reverse()
  72. print(f"\n\033[33m结果排序:{results}\033[0m")
  73.  
  74. except Exception as e:
  75. print(f"\033[31m发生错误:{e}\033[0m")
  76.  
  77.  
  78.  
  79.  
  80. class Customize:
  81. def __new__(cls, Array, reverse: bool):
  82. print("\033[32m我是构造函数\033[0m\n")
  83. instance = super().__new__(cls)
  84. return instance
  85.  
  86. def __init__(self, Array, reverse: bool):
  87. print("\033[32m我是初始化函数\033[0m")
  88. self.Array = Array
  89. self.reverse = reverse
  90.  
  91. def __iter__(self):
  92. return self
  93.  
  94. def __next__(self):
  95. if len(self.Array) == 0:
  96. raise StopIteration
  97. else:
  98. if self.reverse:
  99. return self.Array.pop(0)
  100. else:
  101. return self.Array.pop()
  102.  
  103.  
  104. customize = Customize([v for v in range(1, 41)], reverse=True)
  105.  
  106. custs = []
  107. print("\n\033[32m%s构造函数%s\033[0m" % ("-"*10, "-"*10))
  108. for cust in customize:
  109. custs.append(cust)
  110. if len(custs) == 10:
  111. print(f"{custs[:10]}")
  112. elif len(custs) == 20:
  113. print(f"{custs[10:20]}")
  114. elif len(custs) == 30:
  115. print(f"{custs[20:30]}")
  116. elif len(custs) == 40:
  117. print(f"{custs[30:41]}")
  118.  
  119.  
  120. print("\n\033[32m%s反向循环%s" % ("-"*10, "-"*10))
  121.  
  122. custos = []
  123. for n in Customize([v for v in range(41, 61)], reverse=False):
  124. custos.append(n)
  125. if len(custos) == 10:
  126. print(f"{custos[:10]}")
  127. elif len(custos) == 20:
  128. print(f"{custos[10:20]}")
  129. elif len(custos) == 30:
  130. print(f"{custos[20:30]}")
  131. elif len(custos) == 40:
  132. print(f"{custos[30:41]}")
  133.  
  134.  
  135. class Callable:
  136. def __init__(self, *args, **kwargs):
  137. self.args = args
  138. self.kwargs = kwargs
  139.  
  140. def __call__(self):
  141. try:
  142. results = []
  143. results.append([self.args, self.kwargs])
  144. return [result for result in results]
  145. except Exception as e:
  146. print(e)
  147.  
  148.  
  149. callable = Callable([i for i in range(3)], [j for j in range(4)], {"count": i for i in range(3)}) # 注意:这里i未定义,会报错
  150. print(callable())
Success #stdin #stdout 0.07s 14100KB
stdin
Standard input is empty
stdout
----------开始反向循环----------
[40, 39, 38, 37, 36, 35, 34, 33, 32, 31]
[30, 29, 28, 27, 26, 25, 24, 23, 22, 21]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

反向循环结果:[40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
反向循环结束!

----------开始正向循环----------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

正向循环结果:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
正向循环结束!

计算结果:[41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41]

结果排序:[41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41]
我是构造函数

我是初始化函数

----------构造函数----------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

----------反向循环----------
我是构造函数

我是初始化函数
[60, 59, 58, 57, 56, 55, 54, 53, 52, 51]
[50, 49, 48, 47, 46, 45, 44, 43, 42, 41]
[[([0, 1, 2], [0, 1, 2, 3], {'count': 2}), {}]]