This chapter describes the return values in detail:
0x 00 Introduction to return Values
0x 01 Specifies the return value and the implied return value
0x return statement position with multiple return statements
0x 03 return value type
0x 04 Function Nesting
0x 00 Introduction to return Values
- In retrospect, the previous section briefly describes the functions and their various parameters, including a brief description of the difference between print and return, print is simply printed on the console, and return is the return value as the output of the function, you can use the variable to go, Continue to use the return value to do something else.
- The function needs to be defined and then called, and the result of the return statement in the body of the function is the returned value. If a function does not have a reutrn statement, it actually has an implied return statement, the return value is None, and the type is ' Nonetype '.
- The function of the return statement:
- End Function call, return value
0x 01 Specifies the return value and the implied return value
The return statement in the body of the function returns the value when it has a specified return value
When there is no return statement in the function body, the end of the function will implicitly return a none as the return value, the type is Nonetype, and the return, return none is equivalent to return none.
An example of a return value function is specified:
def showplus (x): Print (x) return x + 1 = showplus (6= num + 2print(add) Output result:69
View Code
Implied return None example:
def showplus (x): Print (x) return x + 1 = showplus (6= num + 2print(add) Output result:69
View Code
0x return statement position with multiple return statements
The Python function returns "return value" using the return statement, which can be assigned to other variables for other purposes.
All functions have a return value, and if there is no return statement, return none is called implicitly
A function can have multiple return statements, but only one can be executed, and if no REUTRN statement is executed, return none will be called implicitly as the returned value
If necessary, you can explicitly call return none explicitly return a none (Null object) as the return value, can be abbreviated to return, but the lazy in Python is a virtue, so generally can not write not write
If the function executes the return statement, the function returns immediately, ending the call, and the other statements after the return are not executed.
Example 1:
1 def showplus (x): 2 Print (x) 3 return x + 1 4 print(x + 1) # will the statement execute 5 6Print(showplus (6)) 7 8 9 output Result:10 6 7
View Code
Example 2:
1 defshowplus (x):2 Print(x)#53 returnX + 1#64 returnX + 2#The statement will not be executed either .5 6 Print(Showplus (5))7 8 9 Output Result:Ten5 One6
View Code
Example 3:
1 defGuess (x):2 ifX > 3:3 return "> 3"4 Else:5 return "<= 3"6 7 Print(Guess (10))8 Print(Guess (2))9 Ten One Output Result: A> 3 -<= 3
View Code
Example 4:
# for: else.. Statement (unexpected termination condition)
# indicates that the statement in the else segment will not be executed if the content of the For statement segment loops correctly, and if the For loop is terminated unexpectedly by a break or a return statement during the loop, then the statement in the else segment is not executed.
deffn (x): forIinchRange (x):ifI > 4: returnIElse: Print("{} is not greater than 4". Format (x))Print(FN (3))Print(FN (6) ) Returns the result:3 is notGreater than 4None5
View Code
0x 03 return value type
Return can only return a single value, regardless of what type is defined, but the value can have more than one element.
return [1,3,5] refers to returning a list, which is a list object, 1,3,5 is the element of the list, respectively.
Return 1,3,5 appears to return multiple values, implicitly encapsulated by Python as a meta-ancestor returned
Example 1:
def fn (): return 3 # When a single value is returned, what type of print(FN ())print( Type (fn ())) output result:3<class'int'> # int integer type
View Code
Example 2:
def showlist (): return [1,3,5] # Multi-element, what type of return is Print (Type (showlist ())) Print (showlist ()) Output result:<class'list'>[1, 3, 5] # list Type
View Code
Example 3:
def showlist (): return (2,4,6) # Multi-element, what type of return is Print (Type (showlist ())) Print (showlist ()) Output result:<class'tuple'> # meta-ancestor type (2, 4, 6)
View Code
Example 4:
def showlist (): return 2,4,6 # When multiple values are not specified, type print(Type (showlist ()))print (showlist ()) Output result:<class'tuple'> # Default encapsulation Cheng Yuanju type
View Code
0x 04 Function Nesting
The function has a visible scope (both inside and outside the visible relationship), which is the concept of scope.
An intrinsic function cannot be called directly from outside, throwing an exception nameerror.
Example 1:
def outer (): def Inner (): # can be understood as an intrinsic function print("inner" ) print("outer") outer () Output: outer
View Code
At this point, if you call outer (), only print ("outer") is executed, because inner, although within the outer function, is also a function, and if the function is to be called, it must use the ' function name () ' method.
Example 2:
defouter ():definner ():Print("Inner") Print("outer") inner ()#external cannot reference intrinsic function, intrinsic function is only valid at local scopeoutput, throw exception: Traceback (most recent call last): File"c:/python/return_value.py", line 6,inch<module>inner () nameerror:name'Inner' is notDefined
View Code
Example 3:
def outer (): def inner (): Print ("inner") Print ("outer") Inner () outer () output result: Outerinner
View Code
Summarize:
This section describes the return value of the function, the function of the return value, specifying the difference from when the return value is not specified, the return value type, and the use of the return value when the function is nested.
Python return value