0

this is the code that I want to ask about.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        length_1 = 0
        tmp_1 = l1
        l1_true_val = 0
        l1_end = False

        length_2 = 0
        tmp_2 = l2
        l2_true_val = 0
        l2_end = False

        while(1):
            if l1_end is False:
                l1_true_val = l1_true_val + tmp_1.val * pow(10, length_1)
                if tmp_1.next is not None:
                    tmp_1 = tmp_1.next
                    length_1 = length_1 + 1
                else:
                    l1_end = True
            if l2_end is False:
                l2_true_val = l2_true_val + tmp_2.val * pow(10, length_2)
                if tmp_2.next is not None:
                    tmp_2 = tmp_2.next
                    length_2 = length_2 + 1
                else:
                    l2_end = True
            if l1_end is True and l2_end is True:
                break
        output = l1_true_val + l2_true_val

        devider = 10
        if output == 0:
            Out = ListNode(val=0)
            return Out

        Out = ListNode()
        this_out = Out

        while(output):
            this_out.val = output % 10
            output = output // 10
            if output != 0:
                nxt_out = ListNode()
                this_out.next = nxt_out
                this_out = this_out.next
                #this_out = copy.copy(this_out.next)

        return Out            
        

I'm quite new to Python, but I've learned that = means shallow copy in Python. If it is true, then this_out = this_out.next should be same with this_out = copy.copy(this_out.next). But the output isn't the same.

How could this happen? I referred this doc

Ryan
  • 73
  • 7
  • 3
    `=` doesn't copy anything. It creates a second reference to the same data. It's not *even* a shallow copy. – Silvio Mayolo Apr 27 '23 at 18:30
  • 1
    An assignment `=` is less than a shallow copy, it copies only the reference to the very same object. A shallow copy of a container (e. g. a list) creates a new list containing copies of the references to the same objects as in the other container. – Michael Butscher Apr 27 '23 at 18:30
  • 3
    As the doc you linked says right at the start: **Assignment statements in Python do not copy objects, they create bindings between a target and an object**. A great starting point here is to read: https://nedbatchelder.com/text/names.html – slothrop Apr 27 '23 at 18:31
  • 2
    " but I've learned that = means shallow copy in Python." Whoever told you this **is wrong and should not be listened to**. – Karl Knechtel Apr 27 '23 at 18:38
  • 1
    " but I've learned that = means shallow copy in Python" that is wrong. I have heard people say this before, so I'm not surprised you heard that, but it is wrong. `=` **doesn't copy at all**. Neither shallow nor deep – juanpa.arrivillaga Apr 27 '23 at 18:38
  • A variable is a *reference* (or pointer) to some value. After `Out = ListNode()` is executed somewhere in memory exists a `ListNode` instance and `Out` now points to that memory location in some fashion. The assignment `this_out = Out` is copying something. But what is being copied is **the pointer**. So `this_out` and `Out` now refer to the same `ListNode` instance. Clearly, copying a pointer to an instance is different than first making a copy of the instance itself and then assigning the pointer to the copy to some variable, which is what `copy.copy` does. – Booboo Apr 27 '23 at 19:00

0 Answers0