Python没有指针。或者说,Python中的所有东西都是指针,包括名称、列表中的条目、属性。。。Python是一种“通过引用传递”的语言。在
下面是几个简单的例子:In [1]: a = ['hello', tuple()] # create a new list, containing references to
# a new string and a new tuple. The name a is
# now a reference to that list.
In [2]: x = a # the name x is a reference to the same list as a.
# Not a copy, as it would be in a pass-by-value language
In [3]: a.append(4) # append the int 4 to the list referenced by a
In [4]: print x
['hello', (), 4] # x references the same object
In [5]: def f1(seq): # accept a reference to a sequence
...: return seq.pop() # this has a side effect:
# an element of the argument is removed.
In [6]: print f1(a) # this removes and returns the last element of
4 # the list that a references
In [7]: print x # x has changed, because it is a reference to the same object
['hello', ()]
In [8]: print id(a), id(x)
4433798856 4433798856 # the same
In [9]: x is a # are x and a references to the same object?
Out[9]: True
Python提供了高级构造来完成在C语言中需要使用指针算法的操作,因此您不必担心给定变量是否是指针,就像您不必担心内存管理一样。在