Python是一种高级编程语言,其设计理念之一就是让开发者无需关心底层的内存管理。因此,Python中没有像C或C++那样的指针概念。本文将介绍Python的引用机制以及它与指针的区别。
一、Python的引用机制
1. 变量即引用
在Python中,变量更像是标签或引用,而不是存储数据的容器:
1 2 3 4 5 6 7 8 9 10 11
| x = 10 y = x
print(x) print(y)
y = 20 print(x) print(y)
|
2. 对象与引用
Python中的每个对象都有:
- 身份(id):对象的唯一标识
- 类型(type):对象的类型
- 值(value):对象的值
1 2 3 4 5 6
| x = [1, 2, 3] y = x
print(id(x)) print(id(y)) print(x is y)
|
二、可变对象与不可变对象
1. 不可变对象
不可变对象包括:整数、浮点数、字符串、元组等
1 2 3 4 5 6 7
| x = 10 y = x y = 20
print(x) print(y)
|
2. 可变对象
可变对象包括:列表、字典、集合等
1 2 3 4 5 6 7 8
| x = [1, 2, 3] y = x y.append(4)
print(x) print(y) print(x is y)
|
三、与C++指针的对比
1. C++指针
1 2 3 4
| int x = 10; int* ptr = &x; *ptr = 20; cout << x;
|
2. Python引用
1 2 3 4
| x = 10 y = x y = 20 print(x)
|
3. 列表操作的对比
C++指针:
1 2 3
| int arr1[] = {1, 2, 3}; int* ptr = arr1; *(ptr + 1) = 10;
|
Python引用:
1 2 3
| list1 = [1, 2, 3] list2 = list1 list2[1] = 10
|
四、深拷贝与浅拷贝
1. 浅拷贝
1 2 3 4 5 6 7 8 9
| import copy
original = [[1, 2], [3, 4]] shallow = copy.copy(original)
shallow[0][0] = 99 print(original) print(shallow)
|
2. 深拷贝
1 2 3 4 5 6 7 8 9
| import copy
original = [[1, 2], [3, 4]] deep = copy.deepcopy(original)
deep[0][0] = 99 print(original) print(deep)
|
五、函数参数传递
1. 不可变参数
1 2 3 4 5 6
| def modify(x): x = 10
val = 5 modify(val) print(val)
|
2. 可变参数
1 2 3 4 5 6
| def modify(lst): lst.append(4)
my_list = [1, 2, 3] modify(my_list) print(my_list)
|
六、综合示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
""" Python引用机制综合示例 """
def demonstrate_mutable_immutable(): """演示可变与不可变""" print("=== 可变与不可变 ===")
str1 = "hello" str2 = str1 str2 = "world" print(f"str1: {str1}, str2: {str2}")
list1 = [1, 2, 3] list2 = list1 list2.append(4) print(f"list1: {list1}, list2: {list2}")
def demonstrate_copy(): """演示拷贝""" print("\n=== 拷贝 ===") import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original) shallow[0][0] = 99 print(f"Original: {original}")
original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original) deep[0][0] = 99 print(f"Original: {original}")
def demonstrate_function_args(): """演示函数参数传递""" print("\n=== 函数参数传递 ===")
def func_immutable(x): x = 10
def func_mutable(lst): lst.append(4)
val = 5 func_immutable(val) print(f"val: {val}")
my_list = [1, 2, 3] func_mutable(my_list) print(f"my_list: {my_list}")
def demo(): """演示""" demonstrate_mutable_immutable() demonstrate_copy() demonstrate_function_args()
if __name__ == "__main__": demo()
|
七、注意事项
1. 避免可变默认参数
1 2 3 4 5 6 7 8 9 10 11
| def func_bad(items=[]): items.append(1) return items
def func_good(items=None): if items is None: items = [] items.append(1) return items
|
2. 理解is与==
1 2 3 4 5 6 7 8 9
| x = 1000 y = 1000 print(x == y) print(x is y)
x = 10 y = 10 print(x == y) print(x is y)
|
3. 合理使用拷贝
1 2 3 4 5
| import copy
original = [[1, 2], [3, 4]] backup = copy.deepcopy(original)
|