Python Learning Summary (function Advanced)

Source: Internet
Author: User
Tags wrapper

-------------------Program Operation Principle-------------------
1, the module built-in nameproperty of the main module whose value is Main, import the module whose value is the module name
1, the creation time, the py file is newer than the PYc file, then the new generation PYc.
2, Magic Num, do the pre-run version test, the version is different to regenerate PYc.
3. Pycodeobject object, string in source code, constant value, bytecode instruction, corresponding relation of original code line number.

2. LEGB rules
1. Local: Locally
The current namespace (such as a function, module), and the parameters of the function also belong to variables within the namespace.
2. ecolsing function: namespace for outer nested functions
namespaces for outer nested functions (common in closures).
3, Global: the overall
Global variables, the namespace of the module in which the function is defined.
4. Builtins: Built-in module namespace
1, Python will automatically load a lot of built-in functions, classes, such as Dict,list,type,print, which are located in the builtins module and can be viewed using dir (builtins).
2, in Python, there is a built-in module, the module has a number of commonly used functions; After Python starts, and no code written by the programmer is executed, Python first loads the built-in function into memory. In addition, the functions in the built-in module can be used directly, without adding the built-in module prefixes before them, because the search for identifiers such as functions, variables, and classes is based on the LEGB rule, where B represents the built-in module.

3. Dir function
Python's built-in function dir can be used to view all the name symbols under a namespace. One use is to look at all the properties and methods of a namespace (the namespace here refers to a class, function, module).

4. Garbage collection
1, small integer object pool:
1, the use of integers in the program is very extensive, python in order to optimize the speed, using a small integer object pool, to avoid the frequent application and destruction of memory space for integers.
2, Python's definition of small integers is [-5, 257) These integer objects are set up well in advance and will not be garbage collected. In a Python program, all integers that are in this range use the same object.
2. Large integer Object pool: Each large integer creates a new object.
3, intern mechanism:
Python has such a mechanism--intern mechanism, so that he only occupies a "HelloWorld" occupies the memory space. By reference counting to maintain when to release.

5. Summary
1, small integer [-5,257) Common object, resident memory.
2, single character common object, resident memory.
3, single word, can not be modified, the default open intern mechanism, the common object, the reference count is 0, then destroy.
4, string (containing spaces), can not be modified, did not open intern mechanism, not the common object, the reference count is 0, destroyed.
5, large integers do not share memory, the reference count is 0, destroyed.
6. Note: Numeric types and string types are immutable in Python, which means that you cannot modify the value of the object, and each time you modify the variable, you are actually creating a new object.

6. def directive
1, def func (), in the bytecode instruction is make_function. Python is a dynamic language, and DEF is actually executing an instruction to create a function (class is the instruction to create it), not just a syntax keyword. The function is not created in advance, but only when it is executed.
2, Def func () will create a function object called Func. is actually to create a function object first, and then bind the Func name symbol to this function.

7. Import the search path when importing the package:
1. Path Search:
Import Sys
Sys.path: Viewing the Import package path
2. Import module path when program executes
Sys.path.append ('/home/usr/local/images ')
Sys.path.insert (0, '/home/usr/local/image ')
3, set the Linux import module path
Echo $pythonpath
Export pythonpath= $pythonpath: '/home/usr/local/image '
4, if the program has referenced the module, but the module has been modified. The module needs to be re-imported:
Can be done using reload (module), similar to the case cache after import operation

7. pyc File
1. Concept
The PYc file is a representation of the Pycodeobject object on the hard disk. Generate the PYc file:
2, PYC file three major functions
1, the creation time, the py file is newer than the PYc file, then the new generation PYc file
2. Magic Num Does the pre-run version detection, the version is different from the new production PYc
3. Pycodeobject Object
4, during the run, the compilation result is the Pycodeobject object, will only exist in memory, and when the Python code of this module is executed, it will save the compilation results to the PYc file, so that the next time you do not compile, directly loaded into memory.
5. This Pycodeobject object contains the strings in the Python source code, the constant values, and the bytecode instructions generated by the syntax parsing. The Pycodeobject object also stores the corresponding relationship between these bytecode directives and the original code line number, so that when an exception occurs, you can indicate which line of code is located.

8. import directive
1, import instructions are used to load the module, if necessary, will also do compile. But the import command, but also do an important thing is to import the module code to execute it once, this matter is very important. Python is interpreted as executing, and even functions are created only when they are executed. If the module code is not executed once, then the functions inside the module cannot be created, let alone to invoke these functions.

2、执行代码的另外一个重要作用,就是在这个 module 的命名空间中,创建模块内定义的函数和各种对象的符号名称(也就是变量名),并将其绑定到对象上,这样其他 module 才能通过变量名来引用这些对象。3、Python 虚拟机还会将已经 import 过的 module 缓存起来,放到一个全局 module 集合 sys.modules 中。 这样做有一个好处,即如果程序的在另一个地方再次 import 这个模块,Python 虚拟机只需要将全局 module 集合中缓存的那个 module 对象返回即可。

1. Concept:
An intrinsic function refers to a variable in the scope of an external function (non-global variable), which is called the inner function as a closure.
2, Case:
def counter (start=0):
#自由变量: Define a list
Count = [start]
Def incr ():
Count[0] + = 1
return count[0] #返回列表结果
Return INCR

#变量接受一个函数con = counter(5)#运行接收到的函数print con()print con()

1, reference general function processing
1. Common processing functions:
1. Introduction of logs
2. Function Execution Time Statistics
3. Pre-processing before executing function
4. Cleaning function after function execution
5, permission verification and other scenarios
6. Cache

2. Case: no parameter function
From time import CTime

#修饰器def timefun(func):    def wrappedfunc():        print("%s called at %s"%(func.__name__,ctime()))        return func()    return wrappedfunc@timefundef foo():    print("I am foo")foo()

3, Case: Decorator and closure mixed

  #coding =utf-8from time Import timedef logged (when): Def log (F, *args, **kargs): Print ("fun:%s args:%            R kargs:%r "% (f, args, Kargs)) #%r string while displaying the original object type Def pre_logged (f): Def wrapper (*args, **kargs): Log (f, *args, **kargs) return F (*args, **kargs) return wrapper def post_logged (f): Def WR                Apper (*args, **kargs): Now=time () Try:return f (*args, **kargs) finally:        Log (f, *args, **kargs) print ("Time Delta:%s"% (time ()-now)) return wrapper try: return {"Pre":p re_logged, "POST":p Ost_logged}[when] except Keyerror, E:raise ValueError (e), ' must be "pre" or "POST" ' @logged ("post") def Fun (name): Print ("%s"% (name)) Fun ("Hello word!")  

-------------------built-in function-------------------
1. Concept:
1, build-in function, start the Python interpreter, enter Dir (builtins), you can see a lot of Python interpreter startup after the default load of properties and functions, these functions are called built-in functions, these functions because of the use of more programming, The CPython interpreter implements these functions with the C language, which is loaded by default when the interpreter is started.

2、这些函数数量众多,不宜记忆,开发时不是都用到的,待用到时再help(function), 查看如何使用,或结合百度查询即可,在这里介绍些常用的内建函数。

2. Range
1. Help (range):
Range (stop), List of integers
Range (start, stop[, Step), List of integers
2, Parameter analysis:
1. Start: Counting starts from start. The default is starting from 0. For example, range (5) is equivalent to range (0, 5).
2. Stop: Ends with stop, but does not include stop. For example: Range (0, 5) is [0, 1, 2, 3, 4] without 5.
3, Step: The distance of each jump, the default is 1. For example: Range (0, 5) is equivalent to range (0, 5, 1).
3. Example:
A = range (5)
List (a)

3. Map
1. Help (map):
Map (...)
Map (function, sequence[, sequence, ...]) list
2, Parameter analysis:
1, function: is a functional
2, Sequence: is one or more sequences, depending on the function requires several parameters
3. The return value is a list
3. Syntax:
Each element in the parameter sequence calls a function, which returns a list containing the return value of each function.
4. Example
Map (Lambda x:x*x, [1, 2, 3])
[1, 4, 9]

    #函数需要两个参数    map(lambda x, y: x+y, [1, 2, 3], [4, 5, 6])    [5, 7, 9]    #函数为None,相当于合并参数为元祖    map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])    [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]    #两个序列参数个数不一致时,个数少的补None    map(None, [1, 3, 5, 7, 9], [2, 4, 6])    [(1, 2), (3, 4), (5, 6), (7, None), (9, None)]

4. Filter
1. Help (Filter):
Filter (...)
Filter (function or None, sequence), list, tuple, or string

    Return those items of sequence for which function(item) is true.  If    function is None, return the items that are true.  If sequence is a tuple    or string, return the same type, else return a list.2、参数分析:    1、function:接受一个参数,返回布尔值True或False    2、sequence:序列可以是str,tuple,list3、语法    filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。返回值的类型和参数sequence的类型相同4、示例    filter(lambda x: x%2, [1, 2, 3, 4])    [1, 3]    filter(None, "she")    ‘she‘

5. Reduce
1. Help (reduce):
Reduce (...)
Reduce (function, sequence[, initial]), value

  Apply A function of the arguments cumulatively to the items of a sequence, from left to right, s        O as to reduce the sequence to a single value.  For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]) calculates (((((1+2) +3) +4) +5). If Initial is present, it's placed before the items of the sequence in the calculation, and serves as a default wh    En the sequence is empty.2, parametric analysis: 1, Function: There are two parameters 2, sequence: The sequence can be str,tuple,list 3, initial: Fixed initial value 3, syntax Reduce takes an element from the sequence in turn and invokes function again with the result of the last call to function. The first call to function, if supplied with the initial parameter, calls function with the first element in sequence and initial as a parameter, otherwise the function is called with the first two elements in the sequence sequence. Note that function functions cannot be none. 4. Example reduce (lambda x, Y:x+y, [1,2,3,4]) (lambda x, Y:x+y, [1,2,3,4], 5) Reduce (lambda x, y:x +y, [' AA ', ' BB ', ' cc '], ' dd ') ' DDAABBCC '  

6, Sorted
1. Help (sorted):
Sorted (...)
Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list
2, Parameter analysis:
To customize the CMP comparison function, return three cases:
X<y Return-1
X>y return 1
X==y return 0
3. Example
def cmp_ignore_case (S1, S2):
u1 = S1.upper ()
U2 = S2.upper ()
If U1 < u2:
If U1 > U2:
Return 1

1. Concept:
An iterator is just a container object that implements an iterator protocol.

2, Next ():
1. Return to the next element of the container
2. Stoplteration exception is thrown at the end

3. ITER ():
Data is converted to a drop-down format via ITER, returning to the hit device itself

1. Concept:
The generator is a function that remembers the position of the last time it was returned in the body of the function. A second (or nth) call to the generator function jumps to the middle of the function, and all local variables that were last called remain unchanged.

2. Features:
1. The generator is a function, and the parameters of the function are preserved.
2. When iterating to the next call, the parameters used are left for the first time, that is, the parameters of all function calls are preserved the first time they are called, not the newly created
3. Save Memory

3, Case:
#generation. py
Def gen ():
For x in Xrange (4):
TMP = yield x
if tmp = = "Hello":
Print "World"
Print "Itcastcpp", STR (TMP)

#执行到yield时,gen函数作用暂时保存,返回x的值;tmp接收下次c.send("python"),send发送过来的值,等价c.send(None)>>>from generation import gen>>>c=gen()>>>>>> None1>>>c.send("python")itcastcpp python2

4. Application Scenario:
1. When a very large list is required, in order to reduce memory consumption, you can use the generator
Class A (object):
def Init(self, i):
From time import sleep, time
Sleep (i)
Print (Time ())

    1、for c in [A(i) for i in range(5)] :[]是通过遍历可迭代对象生成一个list    2、for c in (A(i) for i in range(5)) :()是直接返回可迭代对象

5, Summary:
1. Infinite recursion becomes possible
2, greatly reduces the cost of thread or inter-process context switching
3, the user manually specify the thread call, avoid lock overhead


-------------------Functools Function-------------------
1. Concept:
Functools is python2.5 and some tool functions are placed in this package.

2. Operation:
1, Import Functools: reference the corresponding package
2, dir (functools): View the corresponding tool function in the package

1. Partial function (partial function):
1. Concept:
It is easier to call this new function by setting a default value for some parameters of a function and returning a new function.
2. Example:
Import Functools
def showarg (*args, **kw):
Print (args)
Print (kw)

    p1=functools.partial(showarg, 1,2,3)    p1()    p1(4,5,6)    p1(a=‘python‘, b=‘itcast‘)    p2=functools.partial(showarg, a=3,b=‘linux‘)    p2()    p2(1,2)    p2(a=‘python‘, b=‘itcast‘)

2. Wraps function:
1. Concept:
When using adorners, there are some details that need to be noticed. For example, a decorated function is actually a different function (function names and other functions will change). Python's Functools package provides an adorner called wraps to eliminate such side effects.
2. Example:
Import Functools
def note (func):
"Note function" br/> @functools. Wraps (func)

"Wrapper function"
Print (' Note something ')
return func ()
Return wrapper

    @note    def test():        "test function"        print(‘I am test‘)    test()    print(test.__doc__)

Python Learning Summary (function Advanced)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.