Functions are the first class of objects: they can be used as data to pass
1. Can be referenced
2. Can be used as a function parameter
3. Can return a value as a function
4. Elements that can be used as container types
Small example:
deffunc1 ():Print('func1')defFunc2 ():Print('Func2') DiC= { 'func1': Func1,'Func2': Func2,}func= Dic.get ('func1') func ()
Namespaces: A binding relationship between a name and a value, always remember that there is a vertical line on the left that identifies the different scopes
When it takes effect, when it expires, the method of defining the name
Local space, global namespace, built-in namespace
scope: When used, the range of the lookup
Locals ()
Glocals (), System variable __name__, __file__, __doc__
Global scope: Globally valid, can be accessed anywhere, unless deleted, or survived to the end of file execution
Local scope: Locally valid, can only be accessed within the function, after the execution of the function, the release of the
Closed Package
1. Functions defined inside a function
2. The intrinsic function contains a reference to an outer scope instead of a global scope
def func1 (): = 1 def Func2 (): Print(x) return= func1 () func ()
Feature: Carry your own scope and execute first. Wrapped around a layer of
Application scenario: Crawler, decorator
Decorative Device
Open closure principle, open to expansion, closed to modification
What is an adorner:
Tools for decorating something else:
The adorner itself---> any callable object, such as a function
Decorated Object---> Any callable object, such as a function
The principle that adorners should follow
1. Do not modify the source code of the object being decorated
2. Do not modify the calling method of the decorated object
That is, neither definition nor invocation can be modified
# func = Deco (func) def func (): Pass def func (): pass# Adorner is also a function, (), called First, Get the result @res above the func def func (): Pass
Iterators
Iteration: Repeats the last process, and each iteration continues based on the result of the last execution
such as writing documents, saving, and then modifying
objects that can be iterated: obj.__iter__ Method
Iterative object Execution __iter__ methods---> iterators
Iterator object:
Obj.__iter__ method
Obj.__next__ method
Why you should have iterators:
Provides an iterative approach that does not depend on an index
such as files, collections, dictionaries can be iterated
# For Loop principle for inch obj: Pass # Execute First, obj.__iter__ method, then next ()
Advantages and disadvantages of iterators:
Advantages:
1. Provide an iterative approach that does not depend on the index
2. Save memory with only one value in memory
Disadvantages:
1. Unable to get length
2. One-time, only forward, not back
Generator
The function body contains the yield keyword, and the result of the function is the generator object
The generator object is essentially an iterator, so the yield function is
1. Make an iterator to the execution result of the function
2 . Multiple values can be returned, and return can only return one value at a time
3. Pause function operation, save context information
such as analog piping
Yield statement form: Yield 1
Expression form of yield: x = yield
Food = yield Food_list # pause, get send assignment to food, return food_list
Built-in functions
Numeric class:
ABS (), round (), divmod (), Pow ()
bin (), Oct (), Hex ()
chr (), Ord ()
Max (), min (), SUM ()
Variable determination:
ID (), type ()
Data type Factory functions:
dict (), int (), float (), str (), set (), list ()
function function:
Print (), input (), Len (), __import__ (), sorted (), reversed ()
Enumarate (), Range ()
anonymous function Related:
Lambda x, y:x+yprint= f (in)print(res)
""" 1. No Name, recycle 2 immediately after use. parameter, return value 3. Simple scene logic "" "
Zip ()
Max (*args, Key=none)
Sorted (*args, **kwargs)
Functional Programming:
Map (func, *iterables) # func traversal operation iterates over an object
From Functools import reduce
Reduce (function, sequence, initial=none)
Filter (func, *iterables) # filters Func to True
Object-oriented:
Property (), Staticmethod (), Classmethod ()
Hasattr (), GetAttr (), SetAttr (), delattr ()
Issubclass (), Isinstance ()
Super ()
Python Basic theory-functions