困惑于next()和__iter __()

时间:2015-05-17 03:29:35

标签: python iterator next

在尝试实现迭代器时,我对next()感到困惑。我制作了一个简单的测试脚本,迭代器按照我的预期运行:

class Object:
    def __init__(self, name):
        self.name = name
    def prin(self):
        print self.name

class Some:
    def __init__(self):
        self.data = list()
    def __iter__(self):
        return self.SomeIter(self, len(self.data))
    def fill(self, obj):
        self.data.append(obj)
    def printMe(self):
        for entry in self:
            print entry.name

    class SomeIter:
        def __init__(self, some, end):
            self.index = 0
            self.end = end
            self.name = some.data[self.index].name
            self.data = some.data
        def next(self):
            if self.index == self.end:
                raise StopIteration
            else:
                self.name = self.data[self.index].name
                self.index += 1
            return self

########################################################################


someX = Some()

obj1 = Object("A")
obj2 = Object("B")

someX.fill(obj1)
someX.fill(obj2)
someX.fill(obj2)

for obj in someX:
    print obj.name

我得到了#B B"作为输出。这一切都很好。但是我还有一个树类的迭代器。 next()方法的工作原理基本相同。我首先更新实例然后返回它。但是在树迭代器的情况下,第一个元素被跳过。这对我来说很有意义,因为我在更新实例后才返回self。但是,为什么在上述实现的情况下我会得到不同的行为,实例正在更新,然后才返回?

########################################################################
# RIGHT-HAND-CORNER-BOTTOM-UP-POST-ORDER-TRAVERSAL-ITERATOR
########################################################################
    class RBPIter:
        """!
        @brief Right hand corner initialised iterator, traverses tree bottom
                     up, right to left
        """
        def __init__(self, tree):
            self.current = tree.get_leaves(tree.root)[-1] # last leaf is right corner
            self.csi = len(self.current.sucs)-1 # right most index of sucs
            self.visited = list() # visisted nodes
            self.tree = tree
            self.label = self.current.label
########################################################################
        def __iter__(self):
            return self
########################################################################
        def begin(self):
            return self.tree.get_leaves(self.tree.root)[-1]
########################################################################
        def end(self):
            return self.tree.root
########################################################################
        def find_unvisited(self, node):
            """!
            @brief finds rightmost unvisited node transitively dominated by node
            """
            leaves = self.tree.get_leaves(self.tree.root)
            # loop through leaves from right to left, as leaves are listed
            # in order, thus rightmost list elememt is rightmost leaf
            for i in range(len(leaves)-1, -1, -1):
                # return the first leaf, that has not been visited yet
                if leaves[i] not in self.visited:
                    self.label = leaves[i].label
                    return leaves[i]
            # return None if all leaves have been visited
            return None
########################################################################
        def go_up(self, node):
            """!
            @brief sets self.current to pred of self.current,
                         appends current node to visited nodes, reassignes csi
            """
            self.visited.append(self.current)
            self.current = self.current.pred
            if self.current.sucs[0] not in self.visited:
                self.current = self.find_unvisited(self.current)
            self.label = self.current.label
            self.csi = len(self.current.sucs)-1
            self.visited.append(self.current)
########################################################################
        def next(self):
            """!
            @brief advances iterator
            """
            # if current node is a leaf, go to its predecessor
            if self.current.suc_count == 0 or self.current in self.visited:
                self.go_up(self.current)
            # if current node is not a leaf, find the next unvisited
            else:
                self.current = self.find_unvisited(self.current)
            if self.current == self.end():
                raise StopIteration
            return self

编辑1:

我比较了两个迭代器的输出并且它们不同,SomeIter将第一个元素输出2次:

next:  A
A
next:  A
B
next:  B
B
next:  B

另一个迭代器没有:

next:  a
s
next:  s
i
next:  i
r
next:  r
t
next:  t
t
next:  t
s
next:  s
e
next:  e
t
next:  t

否则" next:a"会发生2次

编辑2:

这实际上并没有让任何人担心。对我有意义......

查看这些调用和输出:

someXIter = iter(someX)
print someXIter.next().name
print someXIter.next().name
print someXIter.next().name

输出:

next:  A
A
next:  A
B
next:  B
B

使用此代码:

class SomeIter:
        def __init__(self, some, end):
            self.index = 0
            self.end = end
            self.name = some.data[self.index].name
            self.data = some.data
        def next(self):
            print "next: ", self.name
            if self.index == self.end:
                raise StopIteration
            else:
                self.name = self.data[self.index].name
                self.index += 1
            return self

为什么这对我没用?因为,当第一次调用next()时,它会打印" next:A",然后更新实例,并打印函数调用的返回值,这又是" A& #34 ;.华?为什么它不是" B",因为返回值应该是更新的实例。

1 个答案:

答案 0 :(得分:3)

Python 2.7

要成为迭代器必须实现迭代器协议:

  • 定义obj.__iter__ AND obj.next
  • obj.__iter__必须返回self
  • 提出StopIteration后,对obj.next()next(obj))的后续调用必须提出StopIteration

如果类只定义__iter__,则__iter__必须返回实现迭代器协议的对象。如果类中的项目包含在列表中的内置类型中,__iter___只能返回iter(list)

我想在整个概念中隐含的是迭代器必须跟踪它在迭代中的位置。

如果希望对象是具有不同迭代序列的迭代器,则可以定义不同的方法来跟踪迭代并前进到下一个项目,然后在obj.next()中使用这些方法。

琐碎的例子:

class Thing(object):
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name
    def __repr__(self):
        return 'Thing({})'.format(self.name)

class Some(object):
    def __init__(self):
        self.data = None
        # need something to keep track of the iteration sequence
        self.__index = None
        # type of iteration do you want to do
        self.direction = self.forward
    def __iter__(self):
        # reset the iteration
        self.__index = None
        return self
    def next(self):
        try:
            return self.direction()
        except IndexError:
            raise StopIteration
    def forward(self):
        if self.__index is None:
            self.__index = -1
        self.__index += 1
        return self.data[self.__index]
    def reverse(self):
        if self.__index is None:
            self.__index = 0
        self.__index -= 1
        return self.data[self.__index]

用法

>>> some = Some()
>>> some.data = [Thing('A'),Thing('B'),Thing('C'),Thing('D')]
>>> for thing in some:
    print thing,

A B C D
>>> some.direction = some.reverse
>>> for thing in some:
    print thing,

D C B A
>>> 

所以也许只需保持next简单,并将树遍历逻辑放在不同的方法中。它可能会更容易测试该逻辑。您可以随时添加不同的行为:

    def odd_then_even(self):
        if self.__index is None:
            self.__index = -1
            self.__odd = True
        self.__index += 2
        try:
            return self.data[self.__index]
        except IndexError:
            if not self.__odd:
                raise IndexError
            self.__odd = False
            self.__index = 0
            return self.data[self.__index]

>>> some.direction = some.odd_then_even
>>> for thing in some:
    print thing,


B D A C
>>>

我很难理解你的内部类解决方案是如何工作的但是看起来很糟糕的一件事是你的迭代器对象的next方法正在返回self并且似乎像next一样应该返回序列/集合中的下一个项目。迭代一组事物时,迭代应提供各个事物而不是迭代器对象的修改实例。