1、return Statement is to return the execution result to the place of call , And return the control of the program to
The program runs to the first return Return ( sign out def block ), No more running the second return.
for example :
def haha(x,y): if x==y: return x,y print(haha(1,1)) Corrected : result : such return The tuple returned from the meeting (1, 1) 1 2 3 4 5 6 2、 But it doesn't mean that there can only be one in a function body return sentence , for example :
def test_return(x): if x > 0: return x else: return 0 print(test_return(2)) 1 2 3 4 5 6 3、 Function does not return, Default return One None object .
There is no... In recursive functions return The situation of :
def recurve(a,b): if a%b==0: return b else: gcd(b,a%b) 1 2 3 4 5 analysis :else There is no return There is no exit , This program runs internally , The program has no return value ,
4、 In interactive mode ,return The results will be printed automatically , When running as a script alone, you need print Function can be displayed .
python What is interaction mode in : It ends with 3 individual > Symbol (>>>).>>> Is called Python Command prompt (prompt)
The input line python The code will execute the code , This model is called Python Interactive mode (interactive mode)
5、 By default , meet return The function will return to the caller , however try,finally Except in the case of :
def func(): try: print(666) return 'ok' finally: print(666) print(func()) 1 2 3 4 5 6 7 8
6、 The function returns... As a return value :( It's actually a closure function )
def sum1(*args): def sum2(): x=0 for i in args: x=x+i return x return sum2 sum1(1,2,3) a=sum1(1,2,3) print(a()) result :6=1+2+3 1 2 3 4 5 6 7 8 9 10 11
7、 Returns a list of functions :
def count(): fs = [] for i in range(1,4): def f(): return i*i fs.append(f) return fs f1, f2, f3 = count() print(f1()) print(f2()) print(f3()) Output : 9 9 9
That's all for this sharing , If it helps you , Pay attention before you go ~
一、參考來源python - List of classin
p{margin:10px 0}.markdown-body