Skip to content Skip to sidebar Skip to footer

I Don't Know __iter__ In Python,who Can Give Me A Good Code Example

my code run wrong class a(object): def __iter(self): return 33 b={'a':'aaa','b':'bbb'} c=a() print b.itervalues() print c.itervalues() Please try to use the code, rath

Solution 1:

a. Spell it right: not

def__iter(self):

but:

def__iter__(self):

with __ before and after iter.

b. Make the body right: not

return33

but:

yield33

or return iter([33])

If you return a value from __iter__, return an iterator (an iterable, as in return [33], is almost as good but not quite...); or else, yield 1+ values, making __iter__ into a generator function (so it intrinsically returns a generator iterator).

c. Call it right: not

a().itervalues()

but, e.g.:

for x in a(): print x

or

print list(a())

itervalues is a method of dict, and has nothing to do with __iter__.

If you fix all three (!) mistakes, the code works better;-).

Solution 2:

A few things about your code:

  1. __iter should be __iter__
  2. You're returning '33' in the __iter__ function. You should actually be returning an iterator object. An iterator is an object which keeps returning different values when it's next() function is called (maybe a sequence of values like [0,1,2,3 etc]).

Here's a working example of an iterator:

classa(object):
    def__init__(self,x=10):
        self.x = x
    def__iter__(self):
        return self
    defnext(self):
        if self.x > 0:
            self.x-=1return self.x
        else:
            raise StopIteration

c=a()

for x in c:
    print x

Any object of class a is an iterator object. Calling the __iter__ function is supposed to return the iterator, so it returns itself – as you can see, the a class has a next() function, so this is an iterator object.

When the next function is called, it keeps return consecutive values until it hits zero, and then it sends the StopIteration exception, which (appropriately) stops the iteration.

If this seems a little hazy, I would suggest experimenting with the code and then checking out the documentation here: http://docs.python.org/library/stdtypes.html

Solution 3:

Here is a code example that implements the xrange builtin:

classmy_xrange(object):
    def__init__(self, start, end, skip=1):
        self.curval = int(start)
        self.lastval = int(end)
        self.skip = int(skip)
        assert(int(skip) != 0)

    def__iter__(self):
        return self

    defnext(self):
        if (self.skip > 0) and (self.curval >= self.lastval):
            raise StopIteration()
        elif (self.skip < 0) and (self.curval <= self.lastval):
            raise StopIteration()
        else:
            oldval = self.curval
            self.curval += self.skip
            return oldval

for i in my_xrange(0, 10):
    print i

Solution 4:

You are using this language feature incorrectly.

http://docs.python.org/library/stdtypes.html#iterator-types

This above link will explain what the function should be used for.

You can try to see documentation in your native language here: http://wiki.python.org/moin/Languages

Post a Comment for "I Don't Know __iter__ In Python,who Can Give Me A Good Code Example"