Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __next__() Magic Method

#1
Python __next__() Magic Method

Syntax


object.__next__(self)

The Python __next__ method returns an arbitrary element that represents the “next” element when you iterate over the object on which it is called. For example, if you iterate over my_object using for x in my_object, Python internally calls my_object.__next__() in each loop iteration to determine the next element.

Formally, the __next__() method implements the built-in next() function. For example, if you call next(x) an object x, Python internally calls x.__next__() to determine the next element of the iterable object x.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Background next()


The next(iterator) function is one of Python’s built-in functions—so, you can use it without importing any library.

Return Value: It returns the next value from the iterator you pass as a required first argument. An optional second argument default returns the passed default value in case the iterator doesn’t provide a next value.




Before we learn more about the __next__() dunder method, let’s have a look at a couple of basic next() examples:

users = ['Alice', 'Bob', 'Carl', 'David'] # convert the list to an iterator
users_iterator = iter(users) x = next(users_iterator)
print(x)
# Output: 'Alice' x = next(users_iterator)
print(x)
# Output: 'Bob' x = next(users_iterator)
print(x)
# Output: 'Carl' x = next(users_iterator)
print(x)
# Output: 'David'

Example Custom __next__()


In the following example, you create a custom class Data and overwrite the __init__(), __iter__(), and __next__() methods so that you can create your own iterator over a Data object.

class Data: def __init__(self, data): self.data = data # an iterable def __iter__(self): self.current_index = 0 return self def __next__(self): if self.current_index < len(self.data): x = self.data[self.current_index] self.current_index += 1 return x raise StopIteration
  • __init__() initializes the data attribute that is expected to be an iterable.
  • __iter__() returns the iterator object — the one that implements the __next__() method. In our case, this is the Data object on which it is called itself. We initialize current_index with zero, so we start iterating with the first index of data.
  • __next__() returns the next value after one iteration. We increment the current_index attribute to keep track of the current index of the element in data.

Let’s create a Data object d and an iterator over the data object using the built-in iter() function (that internally calls __iter__())—and start iterating over the object using the built-in next() function (that internally calls __next__()):

d = Data([1, 'Alice', 42, 'finxter']) # Create an iterator
iterator = iter(d) # Dynamically generate the next values - iterate!
print(next(iterator))
print(next(iterator))
print(next(iterator))
print(next(iterator))
print(next(iterator))

The output is as follows: The first four calls result in the expected elements of the data attribute, i.e., 1, 'Alice', 42, and 'finxter'. The fifth call of next() results in a StopIteration error because we have finished iterating over all elements.

1
Alice
42
finxter
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 34, in <module> print(next(iterator)) File "C:\Users\xcent\Desktop\code.py", line 14, in __next__ raise StopIteration
StopIteration

If you hadn’t defined the __iter__() method, Python would’ve raised an error:

TypeError: ‘…’ object is not iterable


If you call iter(x) on an object on which the x.__iter__() dunder method is not defined, Python will raise a TypeError: '...' object is not iterable.

To fix this error, simply define the __iter__() method in the class definition before calling iter() on an object—and make sure that __iter__() returns an iterator object on which the dunder method __next__() is defined!

Here’s an example:

class Data: def __init__(self, data): self.data = data # an iterable d = Data([1, 'Alice', 42, 'finxter']) # Create an iterator
iterator = iter(d)

Here’s the error message:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 10, in <module> iterator = iter(d)
TypeError: 'Data' object is not iterable

References:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2021/12/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
https://www.worthydiets.com/fitology-ket...
Last Post: carlscull
Yesterday 05:27 AM
» Replies: 0
» Views: 73
(Indie Deal) War Giveaways, Akupara, Hum...
Last Post: xSicKxBot
Yesterday 04:56 AM
» Replies: 0
» Views: 8
(Free Game Key) Free DLC for Steam game ...
Last Post: xSicKxBot
Yesterday 04:56 AM
» Replies: 0
» Views: 7
AppleInsider - Safari API bug leaks brow...
Last Post: xSicKxBot
Yesterday 04:56 AM
» Replies: 0
» Views: 6
Microsoft - How AI is being used to impr...
Last Post: xSicKxBot
Yesterday 04:56 AM
» Replies: 0
» Views: 7
Xbox Wire - Get Ready for the Mushroom W...
Last Post: xSicKxBot
Yesterday 04:55 AM
» Replies: 0
» Views: 7
News - The shiny Galarian bird trio are ...
Last Post: xSicKxBot
Yesterday 04:55 AM
» Replies: 0
» Views: 10
(Indie Deal) The GameCreators 2 Bundle, ...
Last Post: xSicKxBot
01-16-2022 08:59 AM
» Replies: 0
» Views: 58
(Free Game Key) Free DLC for Steam game ...
Last Post: xSicKxBot
01-16-2022 08:59 AM
» Replies: 0
» Views: 9
Microsoft - 4 approaches to comprehensiv...
Last Post: xSicKxBot
01-16-2022 08:59 AM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com



Discord Server © SickGaming.net 2012-2021