Python Note Three
Contents
- Iterator protocol
- Objects that implements the
__iter__method,__iter__returns an iterator object, which has a method callednext(in PY3__next__) return its “next value” AndStopIterationraised when no more values to return.
- Objects that implements the
| |
The built-in function
itercan be used to get an iterator from an iterable object.A generator is a kind of iterator that is defined with normal function syntax. Any function contains a
yieldstatement is called a generator.
| |
Generator method
sendworks just like next, except it takes a single argument(the ‘message’ to send)yieldmay now used as an expression:value = (yield oldvalue)- rather than a statement, when the generator returned,
yieldreturns a value, the value send from the outside through send. Ifnextwas used, yield returns None.
Eight Queens: state[0]=3 row 1 column 4 has a queen
| |
- Tell interpreter where to look module
| |
“main program”
__name__ == "__main__"Pretty print
| |
__all__variable: define the public interface of the module, tells the interpreter what it menas to import all the names from this module: eg;from copy import *
__file__property tells module location