a What is a namespace?
#名称空间: The place where the name is stored, three namespaces, (previously the problem x=1,1 stored in memory, where is the name x stored?). Namespaces are where the name X and 1 bindings are stored)
two load order of namespaces
#1, the Python interpreter starts first, so it first loads: built-in namespaces
#2, execute the test.py file, and then load the global namespace on file basis
#3, if a function is called during the execution of a file, the local namespace is generated temporarily
three Search Order of names
Local namespace---> Global namespaces---> Built-in namespaces
#需要注意的是: In the global cannot view local, in the local can view the global
Four Scope
#1, scopes, scopes
-Global scope (the built-in namespace and the global namespace belong to this scope): Global survival, globally valid
-Local scope (local namespace belongs to this range): temporary survival, partially valid
#2, scope relationships are fixed in the function definition phase, regardless of where the function is called
#3, viewing scope: Globals (), locals ()
LEGB representative name Lookup order: Locals, enclosing function, globals, __builtins__
Locals are namespaces within functions, including local variables and formal parameters
enclosing namespaces for outer nested functions (common in closures)
Globals global variables, the namespace of the module in which the function is defined
Builtins the namespace of the built-in module
What is a closure?
#内部函数包含对外部作用域而非全局作用域的引用
#提示: Before we passed the external values to the function by parameters, closures provide another way of thinking
The significance and application of two-closure package
#闭包的意义: The returned function object, not just a function object, also wraps a layer of scope outside the function, which allows the function to take precedence over the scope of its outer envelope wherever it is called
#应用领域: Delay calculation (originally we are the reference, now we are wrapped up)
Decorator is a kind of application scenario of closure function
Why use an adorner?
#开放封闭原则: Closed for modification, open for expansion
Two what is an adorner
The utensil of another person can be any callable object, and the adorner can be any callable object.
The principle of emphasizing adorners: 1 do not modify the source code of the Decorated object 2 does not modify the method of calling the decorated object
Object of the adorner: Add new features to the adorned object, following the 1 and 2 prerequisites
Use of three adorners
Directly above the decorated function, a single line
@deco1
@deco2
@deco3
def foo ():
Pass
Foo=deco1 (Deco2 (Deco3 (foo)))
The concept of an iteration
#迭代是一个重复的过程, the iteration is repeated every time, and the result of each iteration is the initial value of the next iteration
While True: #只是单纯地重复, and therefore not an iteration
l=[1,2,3]
Count=0
While Count < Len (l): #迭代
Print (L[count])
Count+=1
Second, why do we have iterators? What is an iterative object? What is an iterator object?
#1, why do you have iterators?
For sequence types: strings, lists, tuples, we can iterate over the elements that they contain by using an indexed method. But for dictionaries, collections, files, and so on, there is no index, and if you want to take out the elements that are contained inside, you must find an iterative way that does not depend on the index, which is the iterator
#2, what is an iterative object?
An iterative object is an object with a built-in __iter__ method, the obj.__iter__, as follows
' Hello '. __iter__
Open (' A.txt '). __iter__
#3, what is an iterator object?
The result of an iterative object execution obj.__iter__ () is an iterator object
An iterator object is an object that has a built-in __iter__ and a built-in __next__ method.
File type is an iterator object
Open (' A.txt '). __iter__ ()
Open (' A.txt '). __next__ ()
#4, note:
An iterator object must be an iterative object, and an iterative object is not necessarily an iterator object
three use of Iterator objects
Dic={' A ': 1, ' B ': 2, ' C ': 3}
ITER_DIC=DIC.__ITER__ () #得到迭代器对象, the iterator object is __iter__ and __next__, but the iterator. __iter__ () still gets the iterator itself
Iter_dic.__iter__ () is Iter_dic #True
Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
# Print (iter_dic.__next__ ()) #抛出异常StopIteration, or End flag
#有了迭代器, we can not rely on the index iteration to take the value
ITER_DIC=DIC.__ITER__ ()
While 1:
Try
K=next (Iter_dic)
Print (Dic[k])
Except stopiteration:
Break
#这么写太丑陋了, we need to catch the anomaly ourselves, control next,python so cool, can you help me solve it?
Can, see for loop
Four for Loop
#基于for循环, we can completely no longer rely on the index to fetch the value.
Dic={' A ': 1, ' B ': 2, ' C ': 3}
For K in DiC:
Print (Dic[k])
#for循环的工作原理
#1: Executes the dic.__iter__ () method of the in post object to get an iterator object Iter_dic
#2: Perform next (iter_dic), assign the resulting value to K, and then execute the Loop body code
#3: Repeat procedure 2 until the exception stopiteration is caught, ending the loop
Five advantages and disadvantages of iterators
#优点:
-Provides a unified, index-independent iterative approach
-Lazy calculation, save memory
#缺点:
-Unable to get the length (only if you know how many values are in next complete)
-Disposable, can only go back, can not forward
a What is a generator, the generator is an iterator
#只要函数内部包含有yield关键字, the result of the function name () is the generator, and the function internal code is not executed
def func ():
Print (' ====>first ')
Yield 1
Print (' ====>second ')
Yield 2
Print (' ====>third ')
Yield 3
Print (' ====>end ')
G=func ()
Print (g) #<generator object func at 0x0000000002184360>
Two-process function
#yield关键字的另外一种使用形式: Yield in the form of an expression
Def eater (name):
Print ('%s ready to start eating '%name)
Food_list=[]
While True:
Food=yield food_list
Print ('%s ate%s '% (Name,food))
Food_list.append (food)
G=eater (' Egon ')
G.send (None) #对于表达式形式的yield, when used, the first time must pass None,g.send (none) equal to next (g)
G.send (' steamed lamb ')
G.send (' Steamed velvet antler ')
G.send (' Steamed bear paw ')
G.close ()
G.send (' Roast vegetarian goose ')
yield Summary
#1, make the function an iterator
#2, compare returns, you can return multiple values, you can suspend/save the function's running state
Process-oriented programming
#1, first emphasize: process-oriented programming is definitely not a function of programming so simple, process-oriented is a kind of programming ideas, ideas, and programming ideas are not dependent on specific language or grammar. The implication is that even if we don't rely on functions, we can write programs based on process-oriented thinking.
#2, definition
Process-oriented core is the process of two words, the process refers to the steps to solve the problem, that is, what to do before doing
Process-oriented programming is like designing a pipeline, which is a mechanical way of thinking.
#3, Advantages: The complexity of the problem flow, and then simplify
#4, disadvantage: Poor scalability, modify any stage of the pipeline, will reaching
#5, Applications: Scenarios with low scalability requirements, typical cases such as the Linux kernel, git,httpd
#6, examples
Line 1:
User input user name, password---> user authentication---> Welcome screen
Line 2:
User Input SQL--->sql parsing---> Execution function
a Three -dimensional expression
Name=input (' name >>: ')
res= ' SB ' if name = = ' Alex ' Else ' NB '
Print (RES)
two List-derived
#1, example
Egg_list=[]
For I in range (10):
Egg_list.append (' egg%s '%i)
egg_list=[' egg%s '%i for I in range (10)]
#2, grammar
[Expression for item1 in Iterable1 if Condition1
For item2 in Iterable2 if Condition2
...
For itemn in Iterablen if Conditionn
]
Similar to
Res=[]
For item1 in Iterable1:
If Condition1:
For item2 in Iterable2:
If Condition2
...
For Itemn in Iterablen:
If Conditionn:
Res.append (expression)
#3, Advantages: Convenient, changed the programming habits, can be called declarative programming
three Builder Expression
#1, the [] conversion of the list derivation to () is the generator expression
#2, Example: Raw a basket of eggs to give you an old hen, when used to lay eggs, which is the characteristics of the generator
>>> chicken= (' egg%s '%i for I in range (5))
>>> Chicken
<generator Object <genexpr> at 0x10143f200>
>>> Next (chicken)
' Egg 0 '
>>> list (chicken) #因chicken可迭代, so it can be turned into lists
[' Egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ',]
#3, Advantages: Save memory, generate only one value in memory at a time
Python fourth day