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” AndStopIteration
raised when no more values to return.
- Objects that implements the
|
|
The built-in function
iter
can 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
yield
statement is called a generator.
|
|
Generator method
send
works just like next, except it takes a single argument(the ‘message’ to send)yield
may now used as an expression:value = (yield oldvalue)
- rather than a statement, when the generator returned,
yield
returns a value, the value send from the outside through send. Ifnext
was 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