Home > Backend Development > Python Tutorial > Detailed example of Python reference passing by value concept

Detailed example of Python reference passing by value concept

黄舟
Release: 2017-10-09 10:31:49
Original
1784 people have browsed it

This article mainly introduces the concept and usage of Python reference by value, briefly analyzes the concept and function of Python reference by value, and summarizes and analyzes the specific implementation and usage of Python reference by value in the form of examples. Friends who need it can Refer to the following

The examples in this article describe the concept and usage of Python reference passing by value. Share it with everyone for your reference, the details are as follows:

The parameter value of the Python function is passed by reference, which means that is passed the memory address value of the parameter, so in the function Changing the value of a parameter within the function will also change outside the function.

What needs to be noted here is thatIf the passed parameter type is immutable, such as String type or tuple type, if the value of the parameter needs to be changed within the function, it is equivalent to creating a new object.


# 添加了一个string类型的元素添加到末尾
def ChangeList(lis):
  lis.append('hello i am the addone')
  print lis
  return
lis = [1, 2, 3]
ChangeList(lis)
print lis
Copy after login

The result is:


[1,2,3, 'hello i am the addone']
[1,2, 3,'hello i am the addone']
Copy after login


def ChangeString(string):
  string = 'i changed as this'
  print string
  return
string = 'hello world'
ChangeString(string)
print string
Copy after login

String is not possible Changing the type, the result is:


i changed as this
hello world
Copy after login

The above is the detailed content of Detailed example of Python reference passing by value concept. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template