What is the difference between iter (x) and x .__ iter __ ()?

What is the difference between iter(x)and x.__iter__()?

In my opinion, both of them return an object listiterator, but in the example below, I notice that they are not equal:

x = [1, 2, 3]
y = iter(x)
z = x.__iter__()
y == z
False

Is there something I don't understand about iterator objects?

+4
source share
5 answers

Iter objects do not have equality based on such things.

See what iter(x) == iter(x)returns False. This is because the iter function (which calls __iter__) returns an iter object that does not overload __eq__and therefore returns Truewhen two objects are the same.

No overload ==matches the comparison is.

, x.__iter__().__class__ is iter(x).__class__ , .

+7

iter(x) x.__iter__(). 2 listiterator . , , , .

>>> test = [1,2,3,4]
>>> iter(test)
<listiterator object at 0x7f85c7efa9d0>
>>> test.__iter__()
<listiterator object at 0x7f85c7efaa50>

, 2 .

, iter(test).

, .

>>> test = [1,2,3,4]
>>> iter_one = iter(test)
>>> iter_two = iter_one
>>> print iter_one == iter_two
True
>>> iter_one.next()
1
>>> iter_two.next()
2

, .

, , .

>>> print list(iter(test)) == list(test.__iter__())
True
+1

. -

iter (o [, ])

-. - . o , ( __iter__()) ( __getitem__() , 0).

, , iter() __iter__(). , , , , .

-

In [13]: class CA:
   ....:     def __iter__(self):
   ....:         print('Inside __iter__')
   ....:         return iter([1,2,3,4])
   ....:

In [14]: c = CA()

In [15]: iter(c)
Inside __iter__
Out[15]: <list_iterator at 0x3a13d68>

In [16]: c.__iter__()
Inside __iter__
Out[16]: <list_iterator at 0x3a13908>    #see that even the ids are different for the list_iterator objects.

In [17]: class BA:
   ....:     def __getitem__(self,i):
   ....:         print('Inside __getitem__')
   ....:         return i+5
   ....:

In [18]: b = BA()

In [19]: iter(b)
Out[19]: <iterator at 0x3a351d0>

In [20]: x = iter(b)

In [21]: next(x)
Inside __getitem__
Out[21]: 5

In [23]: next(x)
Inside __getitem__
Out[23]: 6

, , iter() , -

In [24]: i = iter(c)
Inside __iter__

In [25]: j = iter(c)
Inside __iter__

In [26]: for x in i:
   ....:     pass
   ....:

In [27]: next(i)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-27-bed2471d02c1> in <module>()
----> 1 next(i)

StopIteration:

In [28]: next(j)
Out[28]: 1

, i , j , , ( ).

+1

Python, iter() , __eq__, Python , , , .

>>> type(iter([])).__eq__
<method-wrapper '__eq__' of type object at 0x100646ea0>
>>> object.__eq__
<method-wrapper '__eq__' of type object at 0x100650830>

, :

class A(object):
    def __iter__(self):
        return self

    def next(self):
        pass

a = A()
y = iter(a)
z = a.__iter__()
print y == z # will print True

True , , .

iter(obj) obj.__iter__(), __iter__ __iter__ :

class A(object):
    def __init__(self):
        self.__iter__ = lambda: iter(())
    def __iter__(self):
        return iter([])


a = A()
print a.__iter__()
print iter(a)
# output
<tupleiterator object at 0x10842b250>
<listiterator object at 0x10842b2d0>
+1

, iter (x), Iterator, . a = iter(x) b = iter(x), , . Iterator __eq__() , , iter(x) == iter(x) false.

iter() __ iter __() :

Without the second argument, o must be a collection object that supports the iteration protocol ( __ iter __ () method ), or it must support the sequence protocol

0
source

Source: https://habr.com/ru/post/1599336/


All Articles