首页 > python在什么情况下会引用传递呢?

python在什么情况下会引用传递呢?

我写了一个对单链表进行排序的程序,核心部分如下:

class Solution:
def insertionSortList(self, head):
    ans = None
    cur = head

    while cur is not None:
        if ans is None or ans.val > cur.val:
            tmp = ListNode(cur.val)
            tmp.next = ans
            ans = tmp
            cur = cur.next
            continue
        ptr = ans
        while ptr.next is not None and cur.val >= ptr.next.val:
            ptr = ptr.next
        tmp.val = ListNode(cur.val)
        tmp.next = ptr.next
        ptr.next = tmp
        cur = cur.next
    return ans

tmp.next = ptr.next之后这两者指向了同样的对象(初始都是None)然后我发现后面ptr.next = tmp使得tmp.next指向了tmp自己。
在命令行中我写了如下几句:

a = None
b = a
a = 1

然而此时的b并没有改变,仍然是None。难道这与python的小整数缓存池有关吗?

None在python底层是如何表示的呢?我该怎么修改我的代码才能达到目的呢?


1、引用
类型可变的情况下会传递引用,不可变传递值

3、
关于None:
http://www.cnblogs.com/BeginMan/p/3153983.html

【热门文章】
【热门文章】