python _iter__不适用于@classmethod

时间:2018-03-11 17:45:15

标签: python

这个程序运作正常

 class Iteration:
    def __init__(self,*data):
        self.data = data
        self.index = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index >= len(self.data):
            raise StopIteration()
        data = self.data[self.index]
        self.index += 1
        return data
class Iterable(Iteration):
    lis=[]
    def __init__(self):
        self.data = [1,3,4,5,3]
    def __iter__(self):
        return Iteration(self.data)

for i in Iterable():
    print(i)
output:
[1, 3, 4, 5, 3]

当我试图运行此程序时没有运行

class Iterator:
    def __init__(self,data):
        self.data = data
        self.index = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index >= len(self.data):
            raise StopIteration()
        data = self.data[self.index]
        self.index += 1
        return data
class Iterable(Iterator):
    lis = []
    @classmethod
    def _range(cls,data):
        if data == -1:
            return cls(sorted(Iterable.lis))
        if data != -1:
            Iterable.lis.append(data)
        data = Iterable._range(data-1)
    def __init__(self,data):
        self.data = data
        print(self.data)
    def __iter__(self):
        return Iterator(self.data)
i = Iterable._range(10)
output:
None
for i in Iterable.range(10):
    print(i)

输出:

   TypeError
   Traceback (most recent call last)
      <ipython-input-219-982b178ff9a6> in <module>()
      1 i = Iterable._range(10)
      2 print(i)
----> 3 for i in Iterable._range(10):
      4     print(i)

   TypeError: 'NoneType' object is not iterable

我试图通过使用range()装饰器和特殊方法dunder(iter)来复制python classmethod函数,当我尝试运行print(self.data)时,我尝试正确显示输出实例化该类并运行它不起作用的print(i) ...

2 个答案:

答案 0 :(得分:0)

我们当然有range而且不需要这样做。

但是当你正在研究它时,为什么不这样做呢:

class Iterator:
    def __init__(self, limit):
        self.data = list(range(limit)) 
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == len(self.data):
            raise StopIteration()
        data = self.data[self.index]
        self.index += 1
        return data

    @property
    def last(self):
        return self.data[-1]

#it = Iterator(10)
#while it:
#    print(next(it))

print(Iterator(3).last)
print()

for value in Iterator(5):
    print(value)

答案 1 :(得分:0)

class Iterator:
    def __init__(self,data):
        self.data = data
        self.index = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index >= len(self.data):
            raise StopIteration()
        data = self.data[self.index]
        self.index += 1
        return data
class Iterable(Iterator):
    lis = []
    data1 = 0
    @classmethod
    def range3(cls,data):
        while(not Iterable.data1==data+1):
            Iterable.lis.append(Iterable.data1)
            Iterable.data1 = Iterable.data1+1
        return cls(Iterable.lis)
    def __init__(self,data):
        self.data = data
    def __iter__(self):
        return Iterator(self.data)

输出:

  test = Iterable.range3(12)
  repr(test)
  <__main__.Iterable object at 0x7f72e050e908>'
  for i in Iterable.range3(12):
    print(i)
 0
1
2
3
4
5
6
7
8
9
10
11
12

最后我发现由于递归它没有用。所以我改为while循环......