These all use get
.
D.get(key,default)
will look at the dict D
. If it has key key
it will return D[key]
. If not it returns default
.
D={'a':0}
D.get('a', 4)
> 0
D.get('b', 4)
>4
So the second line
if new_distance < distances.get(neighbor,float('inf')):
checks if the new_distance
is less than the current best option, or if there is no a current best option, it will evaluate to True
(since it's going to be less than infinity).
The third line
unvisited[k] = distances.get(k,float('inf'))
gives unvisited[k]
whatever the current distance is to k
or else infinity if no distance is defined.
Back to the first line
pred=predecessors.get(pred,None)
If predecessors[pred]
is defined, it gives pred = predecessors[pred]
. If not, it sets pred=None
. None
is a standard value used in Python to signal that something doesn't have a value. Any function that doesn't return anything explicitly will instead return None