Examples of Python callback function usage
This example describes the usage of the Python callback function. Share it with you for your reference. The specific analysis is as follows:
I. Baidu encyclopedia's explanation of callback functions:
A callback function is a
Analysis of the Function of the if _ name __= = "_ main _" statement in Python in calling a multi-Process, __name ____ main __
November February 27, 2018
IntroductionRecently, I am going to learn how to use multi-process in Python. When reading related books and online materials, we found that all codes contain if _ name __= = "_ main __", during the experimen
thisstu_register("王山炮",course=‘PY‘, age=22,country=‘JP‘ )But it's never going to happen.stu_register("王山炮",course=‘PY‘,22,country=‘JP‘ )Of course that's not true.stu_register("王山炮",22,age=25,country=‘JP‘ )This is equivalent to 2 times the age assignment, will be an error!Non-fixed parametersIf your function is not sure how many parameters the user wants to pass in the definition, you can use the non-fixed parameterdef stu_register(name,age,*args): #
for the Python function, that is, the x in the example above is a stack frame.For example, how does a function work?def foo (): ... = ... = Bar () ... return def Bar () : ... return ' Hello ' ...Use the DIS module to see the byte code of the function foo () (The content is OK, others are regular):Import d
What is the life cycle of the Django request?To put it simply, find the corresponding function (or class) and return the string (or return the rendered string after reading the HTML) by matching the URL correspondence.Dissected as follows:1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端,请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post,体现在url之中.2. url经过Django中的wsgi,再经过Django的中间件,最后url到过路由映射表,在路由中一条一条进行匹配,,一旦其中一条匹配成功就执行对应的视图函数,后面的路由就不再继续匹配了.3. 视图函数根据客户端的请求查询相应的数据.返回给Djan
returning a value to the caller. Return without an expression is equivalent to returning None.
The syntax can be seen as follows:
def functionname (parameters): "Function _ Document String" function_suite return [expression]
(by default, parameter values and parameter names are matched in the order defined in the function declaration.) )
Next I'll give you a small example:
def printme (str): "Print
allows you to define functions inside a function.def add (A, B):#在函数内部定义的函数, converts the string to an ASCII sum.def getsum (x):S=0For n in x:S+=ord (N)return sReturn Getsum (a) +getsum (b)#调用函数, a bit similar to the meaning of recursion.Res=add (' A ', ' B ')Print (RES)#lambda函数; An expression function that defines an anonymous function that assigns the function
-in function enumerate () to retrieve the index value ). However, for data structures (such as set) that cannot be accessed randomly, the iterator is the only way to access elements.
Another advantage of the iterator is that it does not require you to prepare all the elements in the entire iteration process in advance. The iterator calculates the element only when it iterates to an element. Before or after this, the element may not exist or be destroy
As you may have heard, the function with yield is called generator (generator) in Python, what is generator?
Let's throw away the generator and show the concept of yield with a common programming topic.
How to generate Fibonacci columns
The Fibonacci (FIBONACCI) Number column is a very simple recursive sequence, in addition to the first and second numbers, any one of the numbers can be added by the first t
(' a '). __name__ # View function Returns the name of the function ' a ' >>> test1 (' B '). __name__ # View function return function name ' B ' >>> test1 (' a ') # function returned by the calling function Call
__init__(self, year:int, month:int, day:int): self.year = year self.month = month self.day = day def __init__(self): t = time.localtime() self.__init__(t.tm_year, t.tm_mon, t.tm_mday)Who said no? You cannot implement a simple multiple dispatch by yourself. Guido gave example [1] Seven years ago. Students need to read more books and do not bury them in the pitfalls dug by questions ......
Function overloading is
Think of Python's super () function, pythonsuper
Python-super
Thought of by the super () function of Python
First, let's take a look.super()Function Definition:
super([type [,object-or-type]])Return a **proxy object** that delegates method
.
Introduction: Map
Map is a great little feature, and it is also the key to the fast running of Python parallel code. Explain to unfamiliar people that map comes from the function language Lisp. The map function can map another function in sequence. For example
URLs = [' http://www.yahoo.com ', ' http://www.
I often talk about the differences between python function parameters (this article is required ).
When using python, it is found that when the function parameter is list, the function calls list. append () will change the shape p
__(...)
compression into the same numeric type built-in mandatory Cheng Yuanju
x.__coerce__ (y)
(
6) def bit_length (self) uses a binary representation of how many digits the number is at least
(
7) Division of Def_divmod_ (Self,y) to obtain a tuple consisting of quotient and remainder
x.__divmod__ (y)
For paging
(
8) _DIV_ Division distributor
x.__div__ (y)
(
9) _float_ (convert to floating-point type)
x.__float__ ()
(
Ten) __
Floordiv
__//Operator
x
you should be aware of:1 The required parameter of the function is in front, the default parameter is the following, because if the default parameter is before, the system will identify the second parameter you given as the given value of the first default parameter;2 When the function is composed of multiple parameters, the variable parameters are placed in the front, and the small changes are put in the
() functionThe reduce () function receives a parameter similar to map (), a function f, a list, but behaves differently from map (), and reduce () the incoming function f must receive two parameters, and reduce () calls function f repeatedly on each element of the list, and
Raise Exception
Except
return Sys.exc_info () [2].tb_frame.f_back
def findcaller (self):
"""
Find the stack frame of the "caller so" we can note the source
File name, line number and function name.
"""
f = currentframe ()
#On Some versions of IronPython, Currentframe () returns None if
#IronPython isn ' t run with-x:frames.
If f is not None:
f = f.f_back
RV = "(Unknown file)", 0, "(Unknown function
calls provided by Python.function Callit was in the process of a function call that a problem was encountered . To illustrate, I have defined a function like this to illustrate:def f1 (A,b=1 , * var) : print a, ' \ t ' , B, ' \ t ' , Vardef f2 (A,b=1 , **var) : print a, ' \ t ' , V, \ t ' , var First the normal parameters can be called by location, an
param_test (): B = 5 Res = 0 to I in range (100000000): res = B + I return res If __name__== ' __main__ ': Profile.run (' Param_test () ") >>> ===================================== RESTART = = ================================= >>> 5 function calls in 37.012 seconds #全局变量测试结果: 37 sec Ordered by: Standard name ncalls tottime percall cumtime percall Filename:lineno (
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.