How the iter() method work in the str class?

Warrior199

Why when you call the __next__() method on str it says it does not have this method ...

b = 'hello'
b.__next__()  # give AttributeError: 'str' object has no attribute '__next__'
a = iter(b)
a.__next__() # output == 'h'
    

Does not the __iter__() method return self? Well, if it returns self, it is a string that does not have the __next__() method?

So how does return "h"?

chepner

iter only returns its argument if the value is an iterator. str is not an iterator; it is an iterable whose __iter__ method returns a str_iterator object.

>>> a = iter(b)
>>> type(a)
<class 'str_iterator'>

The str_iterator object implements __next__, and maintains iteration state separate from any other iterator over the same object.

>>> b = 'hello'
>>> a1 = iter(b)
>>> a2 = iter(b)
>>> next(a1)
'h'
>>> next(a2)
'h'
>>> next(a2)
'e'
>>> next(a2)
'l'
>>> next(a1)
'e'

You could picture str_iterator being defined something like

class str_iterator:
    def __init__(self, s):
        self.s = s
        self.i = 0

    def __iter__(self):
        return self

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

 class str:

     ...

     def __iter__(s):
         return str_iterator(s)

The iterator remembers is position in the string between calls to __next__. The job of __next__ is to advance the "pointer" and to return the character at the correct position.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How does (this) overriding tostring method for LinkedList class work?

How does zip(*[iter(s)]*n) work in Python?

How does .equals Java String Class method work?

How do method references to local class constructors in Java 8 work?

How to Override a Python class's __str__ method on construction with argument?

Class generator without the __iter__() method but object instance works with next()

How to write extension method that work with base class

How to override __iter__() for custom class?

Python 3: How to write a __iter__ method for derived class so that it extends on the behaviour of the base class' __iter__ method

how does pandas.Series.str.get method work?

How can I implement a method to work with &str, Box<str>, Rc<str>, etc.?

Will the str() method in Python work for all types of parameters?

How does an instance method declaration outside a class work in Ruby?

Yielding a dictionary key, value pair in __iter__ method of a class

How to access index in position method of iter?

How to make iter method of a class return a value without running the for loop?

How does iter() work, it's giving "TypeError: iter(v, w): v must be callable"

How does the Python iter() function work?

how does the method df.column.str.replace(' ','_') in pandas dataframe work?

How to work around type parameter of class shadowed by method

How scrapy crawl work:which class instanced and which method called?

How does str.replace() method work?

How does this javascript class and method work?

The str_replace method does not work in php

How does "contains" work? Does it use iter and getitem?

How does .index method of str work in python?

__str__ method for Class LinkedList

Understanding the __iter__() method in terms of a class

fabric how did it to work and using sudo (connection class) method?