1. Namespace Python has three namespaces built-in namespaces: the execution of the print (sum) print (max) global namespace: file generates a global namespace as the Python interpreter launches. Refers to a file-level definition name that will be placed in that space X = 11if x == 11: print (x) Local namespace: A local namespace is generated when the function is called, and is only temporarily bound when the function is called, and is unbound at the end of the call X = 1000def foo (): x = 1 print (x) foo () print (x) Scope: 1. Global scope: built-in namespace, global namespace, globally valid, can be accessed in any location to, unless del is deleted, otherwise surviving to the end of the file. 2. Local scope: Local namespace, locally valid, can only be called at local scope, the call end is invalidated. First Name lookup order: Local namespace--global namespace--built-in namespace X = 1000def func (): x = 2 print (Locals ()) #locals () view names in local scopes print (Globals ()) #globals () View names in global Scopes 2. Function Object 1. Can be referenced 2. 3 can be passed as a parameter. The return value can be a function 4. can be used as a container type element Example: &nbsP;def foo (): print ("From foo") func = foo print (foo) Print (func) foo () func () def foo (): print ("From foo") def bar (func): print (func) func () bar (foo) def foo (): print ("From foo") dic = {' func ': foo} print (dic[' func ']) dic[' func '] () 3. Closure function Closure: 1. defined in the intrinsic function 2. Contains references to external scopes rather than global scopes the intrinsic function is called a closure function Example: def f1 (): x = 1 def &NBSP;F2 (): #f2称作闭包函数 print (x) &NBSP;&NBSP;RETURN&NBSP;F2&NBSP;&NBSP;F&NBSP;=&NBSP;F1 () print (f) #打印f2函数的内存地址 f () #打印1 Example: from Urllib.request import urlopen def index (URL): def get (): return Urlopen (URL). Read () return get oldboy = index (' http://www.baidu.com ') p rint (Oldboy (). Decode (' Utf-8 ')) print (oldboy.__closure__[0].cell_contents) #打印外部引用而非全局引用对象 4. Decorators tools that decorate other objects, decorate add features, and tools refer to functions. The adorner itself is any callable object, and the object being decorated can be any object that can be called. Why do use adorners? The open closure principle, which is closed for modification, is open to expansion. The decorator is designed to add new functionality to a decorated object without modifying its source code and calling method Adorner equivalent to closure implementation Example: import time def timmer (func): def wrapper (*args , **kwargs): start_time = time.time () res = func ( *args, **kwargs) Stop_time = time.time () print ("run time is {0}". Format (stop_time - start_time)) return wrapper @ timmer #相当于index = timmer (index) def index (): time.sleep (3) print ("Welcome to index") Return 1 index () #其实相当于执行了timmer (Index) Example: import time def timmer (func): def wrapper (*args, **kwargs): start_time = Time.time () res = func (*args, **kwargs) stop_time = Time.time () print ("run time IS&NBSP;{0} ". Format (stop_time - start_time)) return wrapper def index (): time.sleep (3) print (" Welcome to index ") f = timmer (Index) print (f) f () #f () <= ====>wrapper () Authenticated User Login login_user = {' user ': none, ' status ': false} def auth (func): def wrapper (*args, **kwargs): if login_user[' user '] and login_user[' status ': res = func (*args, **kwargs) return res else: name = input ("Please enter Name:") passwd = iNput ("Please enter password:") if name == "Hyh" and passwd == "123": login_user[' user '] = "Hyh" login_user[' status '] = true print ("\033[45mlogin successful\ 033[0m ") res = func (*args, **kwargs) return res else: print ("\033[45mlogin err\033[0m") return wrapper @auth def index (): Print ("Welcome to index page") @auth def home (name): print ("%s welcome to home page " % (name)) Index () home ("Hyh") 5. Iterator iterator concept: The result of repeating the last iteration is the initial value of the next iteration, and the repeating process is called Iteration, one iteration at a time Why do have iterators? onFor data types that do not have an index, you must provide an iterative approach that does not depend on the index iterate objects: built-in __iter__ methods are iterative objects [1,2].__iter__ () ' Hello '. __iter__ () ( ). __iter__ () {"a":1, "B": 2}.__iter__ () {1,2,3}.__iter __ () iterator: executes the __iter__ () method, and the result is an iterator with an iterator __next__ () method &NBSP;&NBSP;&NBSP;I&NBSP;=&NBSP;[1,2,3].__ITER__ () print (i.__next__ ()) #打印1 print (i.__next__ ()) #打印2 print (i.__next__ ()) #打印3 print (i.__next__ ()) #抛出异常 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;I__ITER__ () <==>iter (i) i__next__ () <= =>next (i) how to tell if an object is an iterative object or an iterator object from collections Import iterable, iterator print (isinstance (' abc ', iterable)) print (Isinstance ([],iterable)) print (Isinstance ((), iterable)) print (Isinstance ({' A ': 1},iterable)) print (Isinstance ({1,2},iterable)) f=open (' A.txt ', ' W ') f.__iter__ () print (Isinstance (F, iterable) #只有文件是迭代器对象 iterative objects: only __iter__ Method that executes the method to get the iterator object iteration Protocol: object has __next__ The object has __iter__, and for an iterator object, the __iter__ method is executed, and the result is still itself advantages and disadvantages of iterators Advantages: 1. Provides an iterative approach that does not rely on subscript 2. In terms of the drop iterator itself, it saves memory disadvantage: 1. cannot get the length of an iterator object 2. is not as flexible as the sequence type, is a one-time, can only be taken back to the value, not forward
This article is from the "Linux Technology" blog, so be sure to keep this source http://xiaojishu.blog.51cto.com/4278020/1928374
The advanced function of Python learning