1
n = len(graph)-1
        
def dfs(graph, i, part, res):
    part.append(i)
    
    if i==n:
        res.append(part)
        return 
    
    for j in graph[i]:
        index = len(part)
        dfs(graph, j, list(part), res)
        # part = part[:index]
            
if __name__ == "__main__":
    res = []
    part = []
    graph = [[1,2],[3],[3],[]]

Earlier I was using part = part[:index], as in python list is always passed as reference and thus value gets updated. But, then I came across list(part), it actually removes the appended values in last recursive call, and works as expected.

Can someone explain what actually happens here?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
nolanding
  • 103
  • 8
  • "as in python list is always passed as reference and thus value gets updated." no, nothing is *ever* passed by reference in Python. In any case, `list(part)` creates a copy of the list that was being referenced by `part` – juanpa.arrivillaga Apr 04 '22 at 23:59
  • Maybe look into ```copy``` module. To answer your question, list() returns a new object. So, if you pass a Python list to a ```list(my_list)``` the returned value with be a copy of the original. – alvrm Apr 05 '22 at 00:05
  • "no, nothing is ever passed by reference in Python." can you explain your claim? As when you pass a list in python and keep on appending values, it actually updates the original list. – nolanding Apr 05 '22 at 00:15
  • 1
    If you call `help(list)`, then you will see that `list` is a class. And by `list(part)` you are creating a new list object. The argument of `list` is assumed to be iterable (not necessarily a list object), perhaps internally the values of the given iterable are pushed into the new object sequentially. – Kota Mori Apr 05 '22 at 00:15
  • 1
    And be aware that the behavior is similar to the shallow copy. For example, `x = [[1,2,3], [4,5,6]]; y = list(x); y[0][0] = 9; x` gets `[[9, 2, 3], [4, 5, 6]]`. The original x is updated. – Kota Mori Apr 05 '22 at 00:20
  • @KotaMori can you give an example for the above solution when this shallow copy can create problem? – nolanding Apr 05 '22 at 00:30
  • @nolanding yes, that doesn't imply pass by reference. In pass by reference, suppose you define a function `def foo(&x): x = 0`. Here we using imaginary syntax `&x` in our parameter to indicate the parameter that is being passed by reference. [This mirrors the C++ syntax for pass by reference, which is an example of language that actually supports it](https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-pass-by-reference-c-only). So when you do `y = 42; foo(y); print(y)` it will print `0` because assignments to the parameter in pass by reference *are seen by the caller*. – juanpa.arrivillaga Apr 05 '22 at 00:38
  • @juanpa.arrivillaga I guess this answer helps. https://stackoverflow.com/a/33066581/8747223. – nolanding Apr 05 '22 at 01:12
  • @nolanding yes that is a good answer, but one quibble is that there aren't just 3 evaluation strategies, there are many. – juanpa.arrivillaga Apr 05 '22 at 01:21

0 Answers0