Implementing iterator in Python

Source: Internet
Author: User

today, I share with you python Development implemented in iterator The method steps, hope can help you better learn and use relevant knowledge, come together to see it.

An entity class was created, roughly as follows:

class account():

def __init__(self,

Account_name,

Account_type,

Account_cost,

return_amount=0):

Self.account_name = account_name # account name

Self.account_type = account_type # Account Type

Self.account_cost = account_cost # monthly fee

Self.return_amount = return_amount # return amount

Then create a list of entities:

accounts = [Account (" Zhang San ", " annual fee ", 450.00,

Account (" John Doe ", " monthly knot User ", 100.00),

Account (" Yang does not regret ", " monthly knot user ", 190.00, 25),

Account (" any bank ", " monthly User ", 70.00, 10),

Account (" Ling Feng ", " annual fee ", 400.00, 40)]

I want to perform the next () function, that is, "next" when needed , to get the next element in the list. Test it directly:

results found List does not support the next () feature. Atthis point, List is just a iterable , not a iterator . the differences between iterable and iterator are as follows:

. iterable-- only realizes the object of __iter__;

. iterator-- also implements the objects of the __iter__ and __next__ methods.

where__iter__ returns the iterator object,__next__ returns the next element of the iteration process.

1. make the list a iterator

to make the front accounts List A iterator, simply a iter () function:

Accounts_iterator = iter (accounts)

(Next (Accounts_iterator)). account_name

The results are as follows:

such a simple function, it is estimated that there are still many Python developers don't know, do you?

2. customizing iterator objects

expanding, how do you define your own iterator object? In fact, according to the above definition, the implementation of __iter__ and __next__ methods.

We then define a Accountiterator class:

class accountiterator():

def __init__(Self, accounts):

self.accounts = accounts # account set

Self.index = 0

def __iter__(self):

return self

def __next__(self):

if self.index >= len (self.accounts):

Raise stopiteration (" head up ...")

Else:

Self.index + = 1

return self.accounts[self.index-1]

Running results such as:

with this toss and go,next () function is realized. python has a lot of unexpected features, but also waiting for us to continue to explore, perhaps this is the charm of Python and geek.

Article Source: Melon Garden plowing

Implementing iterator in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.