python-iterators, generators

Source: Internet
Author: User
Tags iterable

I. iterators

1. You can use the Dir () function to check the internal defined functions of a class
2. Features:
__ITER__ () Get iterator
__next__ () Gets the front of the element

We were straight in? Iterative operation So what exactly is an iterative object This section is mainly for
on the iterative object First we look back? What are the Str, list, tuple, Dict, set. So why can we call them an iterative object iterative protocol What is an iterative protocol First we look first? Segment error code :

# pair S = "abc" for C in S:    print (c) # Wrong for I in 123:    print (i) Result: Traceback (most recent call last):    File "/users/ sylar/pycharmprojects/oldboy/iterator.py ", line 8, in<module> for    i in 123:typeerror: ' int ' object was not Itera ble

  Note that there is such a word in the wrong message . ' int ' object is not iterable. translated is an integer type object.
is non-iterative . iterable table? can be iterated . table? An iterative protocol . so how to enter? Verify that your data type is
Conform to an iterative protocol . we can use the Dir function to see all the methods defined in the class:

s = "My hahaha" Print (dir (s)) # can print objects in the? method and function Print (dir (str)) # can also be printed in the class declared in the method and function

  In the print results . look for __iter__ If you can find it . so the object of this class is an iterative object.

[' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ' , ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init_ _ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce ' __ ', ' __reduce_ex__ ', ' __repr__ ', ' __rmod__ ', ' __rmul__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' Capitalize ', ' casefold ', ' center ', ' count ', ' encode ', ' endswith ', ' expandtabs ', ' find ', ' format ', ' Format_map ', ' Index ', ' Isalnum ', ' isalpha ', ' isdecimal ', ' isdigit ', ' isidentifier ', ' islower ', ' isnumeric ', ' isprintable ', ' isspace ', ' Istitle ', ' isupper ', ' join ', ' ljust ', ' lower ', ' lstrip ', ' Maketrans ', ' partition ', ' replace ', ' rfind ', ' rindex ', ' rjust ', ' Rpartition ', ' rsplit ', ' Rstrip ', ' Split ', ' splitlines ', ' startswith ', ' strip ', ' swapcase ', ' title ', ' Translate ', ' upper ' ', ' Zfill '] 

we found that __iter__ can be found in the string. keep looking? list, tuple, dict, set

Print (dir (tuple)) Print (dir (list)) Print (Dir (open ("Protect young women's tender model. txt")) #? Object Print (Dir (set)) Print (dir (dict)) Result: [' __add_ _ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute __ ', ' __getitem__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __ Len__ ', ' __lt__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rmul__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' count ', ' index ' [' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __ Hash__ ', ' __iadd__ ', ' __imul__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __reversed__ ', ' __rmul__ ', ' __setattr__ ', ' __setitem_ ' _ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' append ', ' clear ',' Copy ', ' Count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ' [' _chunk_size ', ' __class__ ', ' __del__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __enter__ ', ' __eq__ ', ' __exit__ ', ' __format__ ', ' __ge__ ', ' __ getattribute__ ', ' __getstate__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __lt__ ' , ' __ne__ ', ' __new__ ', ' __next__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' _ _subclasshook__ ', ' _checkclosed ', ' _checkreadable ', ' _checkseekable ', ' _checkwritable ', ' _finalizing ', ' buffer ', ' Close ', ' Closed ', ' detach ', ' encoding ', ' errors ', ' Fileno ', ' flush ', ' isatty ', ' line_buffering ', ' mode ', ' name ', ' Newlines ', ' read ', ' readable ', ' readline ', ' readlines ', ' seek ', ' seekable ', ' Tell ', ' truncate ', ' writable ', ' write ', ' Writelines ' [' __and__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __iand__ ', ' __init__ ', ' __init '_subclass__ ', ' __ior__ ', ' __isub__ ', ' __iter__ ', ' __ixor__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __or__ ' ', ' __rand__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __ror__ ', ' __rsub__ ', ' __rxor__ ', ' __setattr__ ', ' __sizeof__  ', ' __str__ ', ' __sub__ ', ' __subclasshook__ ', ' __xor__ ', ' Add ', ' clear ', ' copy ',
' Difference ', ' difference_update ', ' discard ', ' intersection ', ' intersection_update ', ' isdisjoint ', ' issubset ', ' Issuperset ', ' Pop ', ' Remove ', ' symmetric_difference ', ' symmetric_difference_update ', ' Union ', ' Update ' [' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute_ ' _ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __ ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' clear ', ' copy ', ' Fromkeys ', ' get ', ' items ', ' Keys ', ' Pop ', ' Popitem ', ' setdefault ', ' Update ', ' values '

   We found this one to go in?ForThere's a cycle of East.__iter__Function,IncludingRangealso have.OK?? Try?
Under We can determine __iter__ function So we think this object adheres to the iterative protocol this __iter__ we make? __next__ () elements in an iterator So what we said earlier for What is the principle? continue to see code

s = "I love Beijing Cheonan?" c = s.__iter__ () # Gets the iterator print (c.__next__ ()) # iterator into the iteration. Get an element I print (c.__next__ ()) # Love Print (c.__next__ ()) # North Print (c.__next__ ()) # Beijing print (c.__next__ ()) # Days print (c.__next__ ()) # Print (c.__next__ ()) # Print (c.__next__ ()) # Stopiteration

   We can take the content that we want to iterate as a bullet..And then what.Get to Iterator__iter__ (), just put the bomb in the magazine.
__next__ () Every one of the bombs ( element typed Span class= "Fontstyle1". that is cycle time The beginning of the
time is __iter__ () after every acquisition element is done by __next__ () When the program encounters
stopiteration

?" What is the builder python There are three types to get the generator

1. Pass the builder function
2. 3. Data conversion can also be obtained? Builder

/span>

&NBSP;

python-iterators, generators

Related Article

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.