Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shallowcopy of an object
#1
Can somebody explain to me why in the following code, temp is equal to res after the loop body statement? I have tested it with input [1] and after the loop executes temp is as I said equal to the new res. Also, res = res + temp does not produce the expected [[1],[]] but [[1]].
Can somebody explain this to me as well?

def driver(arr):
    res = [[]]
    def subsets(arr,res):
        if arr == []:
            return
        subsets(arr[1:],res)
        temp = res
        #here temp = [[]] for the inner most function call
        for subset in res:
            subset.append(arr[0])
        #but here temp = res if one tries for instance the input [1]
        res = res + temp
    subsets(arr,res)
    return res
Reply


Messages In This Thread
shallowcopy of an object - by usercat123 - Feb-04-2022, 08:30 PM
RE: shallowcopy of an object - by deanhystad - Feb-05-2022, 05:13 AM
RE: shallowcopy of an object - by usercat123 - Feb-05-2022, 01:52 PM
RE: shallowcopy of an object - by deanhystad - Feb-05-2022, 06:00 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020