In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "Python function value transfer, reference transfer, what is the difference between formal parameters and actual parameters", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python function value transfer, reference transfer, what is the difference between formal parameters and actual parameters"!
In general, when defining a function, we will choose the function form with parameters, and the function of the function parameter is to transfer data to the function and make it do specific operations on the received data.
When using a function, formal parameters (formal parameters for short) and actual parameters (actual parameters for short) are often used, both of which are called parameters. The difference between them is:
Formal parameters: when defining a function, the parameters in parentheses after the function name are formal parameters, for example:
# when defining a function, the function parameter obj here is the formal parameter def demo (obj) print (obj)
Actual parameters: when calling a function, the parameters in parentheses after the function name are called actual parameters, that is, the parameters given to the function by the caller of the function. For example:
A = "python" # call the defined demo function, and the function parameter a passed in is the actual parameter demo (a)
The difference between an actual parameter and a formal parameter is like choosing a protagonist in a screenplay, the role in the script is equivalent to the formal parameter, and the actor who plays the role is equivalent to the actual reference.
After you understand what formal parameters and actual parameters are, let's think about another question, that is, how are actual parameters passed to formal parameters?
In Python, according to the type of actual parameters, function parameters can be passed in two ways, namely, value passing and reference (address) passing:
Value passing: suitable for argument types that are immutable (strings, numbers, tuples)
Reference (address) passing: applicable to argument types that are variable types (list, dictionary)
The difference between value transfer and reference transfer is that after the function parameter is passed, if the value of the parameter changes, it will not affect the value of the parameter; while after the function parameter continues to reference and pass, change the value of the parameter, the value of the parameter will also change.
For example, define a function named demo, passing in a variable of type string (representing value passing) and a variable of type list (representing reference passing):
Def demo (obj): obj + = objprint ("formal parameter value is:", obj) print ("- value passing -") a = "python" print ("a value is:", a) demo (a) print ("argument value is:", a) print ("- reference transfer -") a = [1Jing 2jue 3] print ("a value is:", a) demo (a) print ("argument value is:", a)
The running result is:
-value transfer-
The value of an is: python
The parameter value is: pythonpython
The actual parameter value is: python
-reference passing-
The value of an is: [1,2,3]
The parameter values are: [1, 2, 3, 1, 2, 3]
Actual parameter values are: [1, 2, 3, 1, 2, 3]
It is not difficult to see that when the value transfer is performed, the actual parameters will not change if the value of the formal parameters is changed, while when the formal parameters are changed, the actual parameters will also change.
At this point, I believe you have a deeper understanding of "what is the difference between Python function value passing, reference passing, formal parameters and actual parameters". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.