1

I was taught that a += b equals a = a + b. I got one essay: https://www.python.org/doc/essays/graphs/. And there the result differs depending on I use path = path + [start] or path += [start]. In the first case I have the expected result [['a', 'b', 'c', 'd'], ['a', 'b', 'd'], ['a', 'c', 'd']]. But in another case the output is different: [['a', 'b', 'c', 'd']]. Why is it? Here is the code:

graph = {}
graph['a'] = ['b', 'c']
graph['b'] = ['c', 'd']
graph['c'] = ['d']
graph['d'] = ['c']
graph['e'] = ['f']
graph['f'] = ['c']

def find_all_paths(graph, start, end, path=[]):

    path = path + [start]   # result is [['a', 'b', 'c', 'd'], ['a', 'b', 'd'], ['a', 'c', 'd']]
    # path += [start]       # result is [['a', 'b', 'c', 'd']]

    if start == end:
        return [path]

    if not start in graph:       # I'm using python3
    # if not graph.has_key(start):
        return []

    paths = []

    for node in graph[start]:

        if node not in path:

            newpaths = find_all_paths(graph, node, end, path)

            for newpath in newpaths:
                paths.append(newpath)

    return paths

print(find_all_paths(graph, 'a', 'd'))

I tried to check these:

a = 4
b = 5
a += b  # 9

c = 4
d = 5
c = c + d # 9

print(a == c) # True

a_1 = [4]
b_1 = [5]
a_1 += b_1 # [4, 5]

a_2 = [4]
b_2 = [5]
a_2 = a_2 + b_2 # [4, 5]

print(a_1 == a_2) # True

And I am very thankful for your kind help if you could tell me the correct term for this a += b. Is there any description of this case?

taras
  • 6,566
  • 10
  • 39
  • 50

0 Answers0