Home > Backend Development > Python Tutorial > Detailed introduction to python variable passing

Detailed introduction to python variable passing

高洛峰
Release: 2017-03-16 09:29:04
Original
1913 people have browsed it

pythonVariablesPass

Value

  • Code

num_1 = 123
num_2 = num_1
# 改变num_2值前
print 'num_1 = {0}, num_2 = {1}'.format(num_1, num_2)
num_2 = 0
# 改变num_2值后
print 'num_1 = {0}, num_2 = {1}'.format(num_1, num_2)
Copy after login
  • Output

num_1 = 123, num_2 = 123
num_1 = 123, num_2 = 0
Copy after login
  • Conclusion

Numeric variables are passed by value.

String

  • Code

str_1 = 'hello python'
str_2 = str_1
# 改变str_2值前
print 'str_1 = {0}, str_2 = {1}'.format(str_1, str_2)
str_2 = 'hello'
# 改变str_2值后
print 'str_1 = {0}, str_2 = {1}'.format(str_1, str_2)
Copy after login
  • Output

str_1 = hello python, str_2 = hello python
str_1 = hello python, str_2 = hello
Copy after login
  • Conclusion

String variables are also passed by value.

Linked list

  • Code

l_1 = [1, 2, 3, 4]
l_2 = l_1
print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2)
l_2[0] = 100  # 改变l_2的第一个元素
print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2)
l_2 = [1,1,1] # 改变l_2的全部元素
print 'l_1 = {0}, l_2 = {1}'.format(l_1, l_2)
Copy after login
  • Output

l_1 = [1, 2, 3, 4], l_2 = [1, 2, 3, 4]
l_1 = [100, 2, 3, 4], l_2 = [100, 2, 3, 4]
l_1 = [100, 2, 3, 4], l_2 = [1, 1, 1]
Copy after login
  • Conclusion

As you can see from the above output, the functions of l_1 and l_2 are similar to pointers in c/c++, and the function of l_2 = l_1 It is equivalent to l_2 and l_1 pointing to the same memory, and the contents are [1, 2, 3, 4]. When l_2[0] = 100, the first element in l_1 is also changed. l_2 = [1,1,1] makes l_2 point to another piece of memory, which will not affect the content of l_1.

Dictionary

  • Code

d_1 = {'a': 1, 'b': 2, 'c': 3}
d_2 = d_1
print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2)

d_2['c'] = 10
print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2)

d_2 = {'e': 12, 'f': 15}
print 'd_1 = {0}, d_2 = {1}'.format(d_1, d_2)
Copy after login
  • Output

d_1 = {'a': 1, 'c': 3, 'b': 2}, d_2 = {'a': 1, 'c': 3, 'b': 2}
d_1 = {'a': 1, 'c': 10, 'b': 2}, d_2 = {'a': 1, 'c': 10, 'b': 2}
d_1 = {'a': 1, 'c': 10, 'b': 2}, d_2 = {'e': 12, 'f': 15}
Copy after login
  • Conclusion

The output shows that dict and list have the same properties. Direct assignment is similar to passing by reference in C++ .

Object

  • ##Code

class Point:
    def init(self, x, y):
        self.x = x
        self.y = y
    def str(self):
        return ''.join(['x = ', str(self.x), ' ', 'y = ', str(self.y)])

p_1 = Point(12,34)
p_2 = p_1
print 'p_1: {0};  p_2: {1}'.format(p_1, p_2)

p_2.x = 122
print 'p_1: {0};  p_2: {1}'.format(p_1, p_2)

p_2 = Point(89, 978)
print 'p_1: {0};  p_2: {1}'.format(p_1, p_2)
Copy after login
  • Output

p_1: x = 12 y = 34;  p_2: x = 12 y = 34
p_1: x = 122 y = 34;  p_2: x = 122 y = 34
p_1: x = 122 y = 34;  p_2: x = 89 y = 978
Copy after login
  • Conclusion

Custom objects are also passed by reference during the assignment process.

Summary

In Python, numerical values ​​and strings are passed by value during the assignment process; list, dict, and objects are passed by reference by default during the assignment process. If you need to pass by value, you can Use the copy and deepcopy

functions under the copy module.

The above is the detailed content of Detailed introduction to python variable passing. 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