我的 Python 水平一般,看看这个有没有用吧from typing import overload
class custom_range:
@overload
def __init__(self, stop: int) -> None:
...
@overload
def __init__(self, start: int, stop: int, step: int = 1) -> None:
...
def __init__(self, *args) -> None:
if len(args) == 0 or len(args) > 3:
raise SyntaxError()
self._start = 0
self._stop = 0
self._step = 1
if len(args) == 1:
self._stop = args[0]
else:
self._start = args[0]
self._stop = args[1]
if len(args) == 3:
self._step = args[2]
def __iter__(self):
self._current = self._start
return self
def __next__(self) -> int:
if self._current == self._stop:
raise StopIteration()
value = self._current
self._current += self._step
return value
for i in custom_range(12):
print(i, end=' ')
print()
for i in custom_range(12, 24):
print(i, end=' ')
print()
for i in custom_range(12, 24, 3):
print(i, end=' ')
print()
|