1、为什么要实现成一个序列呢?定义一个函数也能达到目的:
def a(n): if n >= -5 and n <= 5: return 1 else: return 0 # usage >>> a(-5), a(0), a(5) (1, 1, 1) >>> a(-6), a(6), a(100) (0, 0, 0)
2、也可以自定义一个类,来模拟一个序列:
class RectWindow(object): def __getitem__(self, key): if not isinstance(key, int): raise TypeError if key >= -5 and key <= 5: return 1 else: return 0 # usage >>> a = RectWindow() >>> a[-5], a[0], a[5] (1, 1, 1) >>> a[-6], a[6], a[100] (0, 0, 0)