Function and generators
Function and generators
Function basics: Why functions?; Coding function (def and return).
Why coding functions?: avoid code redundancy, only one operation that maintain; reuse functions; divide complex systems in manageable part. When does the code nested inside the function definition statement run?: the body is called when the function is call expression; the body runs anew each time the function is called.
Scopes: Python scopes basics; The global statement; Scopes and nested functions; The nonlocal statements; Why nonlocal? State retention options.
def func():
X = 'NI'
def nested():
nonlocal X
X = 'Spam'
nested()
print(X)
func() # prints 'spam' because the nonlocal statement means that the assignment to X inside the nested function changes X in the enclosing function's local scope. Without it X would be classify as local to the nested function, then printed 'NI'
Arguments: Argument-passing basics; Special argument-matching modes; The min wakeup call; Generalized set functions; Emulating the python print function.
def func(a, *pargs): print(a, pargs)
func(1, 2, 3) #prints 1 (2, 3) *pargs collects the remaining positional arguments into a new tuple object.
def func(a, **kargs): print(a, kargs)
func(a=1, c=3, b=2) # print 1 {'b':2, 'c':3} **kargs collects the remaining keyword arguments into a dictionary.
Advanced function topics: Function design concepts; Recursive functions; Function objects attributes and annotations; Anonymous functions lambda; Functional programming tools.
How are lambda expressions and def statements related?: lambda returns a function object and it can be used to nest a function definition; it doesn't support a block statement. What's the point of using lambda?: inline small units of executable code. Compare and contrast
map, filter, reduce
: all apply another function to items in a sequence;map
passes each item to the functions and collects all results;filter
collects items for which the function returns a true value;reduce
computes a single value by applying the function. Function annotations: embellishment of a functions__annotation__
attribute. Recursive functions: call themselves in order to loop. Ways that functions can communicate results to a caller: return statements, changing passed-in mutable arguments, setting global variables.
Comprehension and generations: List comprehensions and functional tools; Generator functions and expressions; Comprehension syntax (list, generator, set, dictionary).
How are generators and iterators related?: generators support iteration protocol automatically, repeatedly advances to the next item is a series; generator function with def and yield; classes and special methods
__iter__
. How identify a generator function?:yield
statement somewhere. What does a yield statement do?: compile the function as a generator; when is running it sends the result back to the caller and suspends the functions state. How are map calls and list comprehension related: map applies a function call to each item.
The benchmarking interlude: Timing iteration alternatives; Timing iteration and python with timeit; Other benchmarking; Function gotchas.
speed: timer an timeit
Última actualización
¿Te fue útil?