Third, multi-tasking - coroutine

First, iterators

In the original basis, to produce a new version.

for temp in 100:

    print(temp)

int is not an iterator type.

from collections import Iterable

Determine whether the iteration, as long as Iterable determine whether it is a subclass can be.

Analyzing with isinstance.

isinstance (a, A) the return value is true, indicating that the object is a class A created by out. E.g:

isinstance ([11,22,33], Iterable) return True. Expressed [11,22,33] can be iterated.

isinstance ( "abc", Iterable) returns True. It represents the "abc" can be iterated.

isinstance (100, Iterable) returns False. It represents 100 non-iterative.

 

Import Iterable the Collections from 
class
Classmate (Object): DEF __init__ (Self): self.names = List () DEF the Add (Self, name): self.names.append (name)
DEF the __iter __ (Self):
"" "If you want to an object called object can iterate, that can be used for, then you must realize __iter__ method "" "
return ClassIterator ()
class ClassIterator (Object):
DEF the __iter __ (Self):
Pass
DEF the __next __ (Self):
Pass Classmate
= Classmate () classmate.add ( " Pharaoh " ) classmate.add ( " king ") Classmate.add ( " Joe Smith " )
Print ( "classmate determine whether the object can be iterated:", isinstance (classmate, Iterable))
for name in classmate: Print (names)

for temp in xxx_obj:

    pass

1, to determine whether xxxx_obj is iterative.

2, under the premise of the establishment of the first step, to get the return value of the function calls iter __iter__ method of xxxx_obj object.

3, the return value iter__ __ method is an iterator.

 

Guess you like

Origin www.cnblogs.com/ql0302/p/11240610.html