Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether the parameter of Python passes a value or a reference

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "whether the parameters of Python are passed or quoted". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In Python Python +, passing values and passing references are two ways to pass parameters of a function. When learning CCMG, some people like to mechanically ask similar questions: "in a function, do parameters pass values or references?" .

Before you answer this question, let's take a look at two pieces of code.

Code snippet 1:

Def foo (arg):

Arg = 2

Print (arg)

A = 1

Foo (a) # output: 2

Print (a) # output: 1

Students who have read code snippet 1 may say that parameters are value passing.

Code snippet 2:

Def bar (args):

Args.append (1)

B = []

Print (b) # output: []

Print (id (b)) # output: 4324106952

Bar (b)

Print (b) # output: [1]

Print (id (b)) # output: 4324106952

After looking at code snippet 2, some people may say, the parameter is passing a reference, so the question is, is the parameter passing a value or a reference or neither?

To figure this out, first understand the relationship between variables and objects in Python.

Variables and objects

In Python, everything is an object, numbers are objects, lists are objects, functions are objects, and everything is an object. A variable is a reference to an object (also known as a name or label), and the operation of the object is done through a reference. For example, [] is an empty list object, and the variable an is a reference to that object

A = [] a.append (1)

In Python, "variable" is more accurately called "name", and the assignment operation = is to bind a name to an object. It's like adding a label to an object. For example:

A = 1

Assigning the integer 1 to the variable an is equivalent to binding a label to the integer 1.

A = 2

The integer 2 is assigned to the variable a, which is equivalent to tearing off the a label from the original integer 1 and attaching it to the integer 2.

B = a

Assigning variable a to another variable b is equivalent to putting two labels on object 2, which can be used to manipulate object 2.

The variable itself does not have type information, and the type information is stored in the object, which is very different from the variable in C + (the variable in C is a memory area).

Function parameter

In the Python function, the parameter passing is essentially an assignment operation, and the assignment operation is a name-to-object binding process. After knowing the nature of assignment and parameter passing, let's analyze the first two pieces of code.

Def foo (arg):

Arg = 2

Print (arg)

A = 1

Foo (a) # output: 2

Print (a) # output: 1

In snippet 1, the variable an is bound to 1, and when the function foo (a) is called, it is equivalent to assigning an arg=1 to the parameter arg, when both variables are bound to 1. After arg is reassigned to 2 in the function, it is equivalent to tearing off the arg tag on 1 and affixing it to 2, while the other label an on 1 exists all the time. So print (a) is still 1.

Let's take a look at code segment 2 again.

Def bar (args):

Args.append (1)

B = []

Print (b) # output: []

Print (id (b)) # output: 4324106952

Bar (b)

Print (b) # output: [1]

Print (id (b)) # output: 4324106952

Before the execution of the append method, b and arg both point to (bind) the same object. When the append method is executed, there is no reassignment operation, so there is no new binding process. The append method just inserts an element into the list object, the object is the same object, but the content of the object has changed. Because both b and arg are bound to the same object, executing b.append or arg.append methods is essentially an operation on the same object, so the content of b changes after the function is called (but id remains the same object)

Finally, back to the question itself, is it a value or a reference? It is not accurate to say that it is inaccurate to pass values or citations. If you have to install an exact name, it is called call by object. If, as an interviewer, you have to check whether the candidate knows how to pass the parameters of the Python function, instead of discussing the literal meaning, it is better to have some actual code.

Show me the codedef bad_append (new_item, averse list = []): a_list.append (new_item) return a_list

This code is the most common mistake for beginners, using a variable (mutable) object as the default value for the parameter. Once the function is defined, the default parameter a_list points (binds) to an empty list object, and each time the function is called, the same object is append. So writing in this way has a potential bug, and the same call returns a different result.

> print bad_append ('one') [' one'] > print bad_append ('one') [' one', 'one']

The correct way is to specify the default value of the parameter as None

Def good_append (new_item, a_list=None): if a_list is None: await list = [] a_list.append (new_item) return a_list

This is the end of the content of "whether the parameters of Python are passed values or references". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 218

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report