Python-fullstack-s13-day13-python Foundation

Source: Internet
Author: User
Tags generator print print

I. Contents of this section

1. Iterators we can use a for loop to iterate over an iterative object, so how do you see an object as an iterative object?
We are now familiar with the iterated objects of STR, list, tuple, Dict, set.
We can use the Dir function to see all the methods defined in an object or class.

1  for inch S: 2     Print (s) 3 4 s1 = 1235# for el in S1:6#     print (S1)  78print(dir (s))9print(dir (S1))

Use Dir to view:

Dir (s) returns the following results:
[' __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 ']

Dir (S1) returns the following results:
[' __abs__ ', ' __add__ ', ' __and__ ', ' __bool__ ', ' __ceil__ ', ' __class__ ', ' __delattr__ ',
' __dir__ ', ' __divmod__ ', ' __doc__ ', ' __eq__ ', ' __float__ ', ' __floor__ ', ' __floordiv__ ',
' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ',
' __index__ ', ' __init__ ', ' __init_subclass__ ', ' __int__ ', ' __invert__ ', ' __le__ ',
' __lshift__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __neg__ ', ' __new__ ', ' __or__ ',
' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rand__ ', ' __rdivmod__ ', ' __reduce__ ', ' __reduce_ex__ ',
' __repr__ ', ' __rfloordiv__ ', ' __rlshift__ ', ' __rmod__ ', ' __rmul__ ', ' __ror__ ', ' __round__ ',
' __rpow__ ', ' __rrshift__ ', ' __rshift__ ', ' __rsub__ ', ' __rtruediv__ ', ' __rxor__ ', ' __setattr__ ',
' __sizeof__ ', ' __str__ ', ' __sub__ ', ' __subclasshook__ ', ' __truediv__ ', ' __trunc__ ', ' __xor__ ',
' Bit_length ', ' conjugate ', ' denominator ', ' from_bytes ', ' imag ', ' numerator ', ' real ', ' to_bytes ']

By comparing the method with the double underline, we can get the __iter__ in an iterative object, but not the method in the non-iterative object.
Even in the open file, there will be __iter__ this method.

In conclusion, we can determine that if the object has a __iter__ function, then we think that this object adheres to the iterative protocol and can iterate.

The __iter__ here help us get an iterator to the object, and we use __next__ () to get to the element in an iterator.
How for Works is resolved, for example:

1s ="I love Beijing Cheonan"2C = S.__iter__()#Get iterators3 Print(c.__next__())#iterate using iterators. Get an element I4 Print(c.__next__())#Love5 Print(c.__next__())#North6 Print(c.__next__())#Beijing7 Print(c.__next__())#days8 Print(c.__next__())#Ann9 Print(c.__next__())# ?Ten Print(c.__next__())#stopiteration Error After completion of iteration

2. Generator
The essence of the generator is the iterator, which in Python has three ways of getting the generator:
1. Through the generator function
2, through a variety of derivation to implement the generator
3, through the conversion of data can also get the generator

We try to use the generator function to complete the creation and use of the generator.

1 def func (): 2     Print ("111") 3     yield 2224 ret = func ()5print(ret)6 Ret1 = ret.__next__()7print(RET1)

Print print Returns the result of <generator object func at 0x0309c420>

We can execute the generator directly by executing __next__ (), and the print results are 111 and 222.

Note, however, that using the __next__ method, when the program runs out of the last yield, the subsequent call to __next__ () will cause an error.

The Send () method, like __next__ (), allows the generator to execute to the next yield
The difference between send () and __next ():
1, send and Next () are to let the generator go down one time;
2. Send can pass a value to the position of the previous yield, cannot send a value to the last yield, cannot use Send () when executing the generator code for the first time.

Python-fullstack-s13-day13-python Foundation

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.