0

Why would these two lines of code do anything differently from each other?

visited_with_path.append(deep_copy(get_path_to_point(from_point)).append(point))

and

    x = deep_copy(get_path_to_point(from_point))
    x.append(point)
    visited_with_path.append(x)

I just spent a whole lot of time debugging until I tried making the above change (from first to second), and although it fixed the problem, I have no idea why. They seem like they should have identical functionality.

If it helps, visited_with_path is a list of lists of points, deep_copy makes a deep copy of this list of lists, get_path_to_point inputs a point and returns a list of points, and from_point and point are just points.

Any thoughts?

2
  • Because .append is in-place, so returns None Commented Sep 26, 2014 at 18:01
  • Even if append returned a reference to the list being appended to, that one-liner is far too unreadable to be recommended. Plus, different people might assume different things: I would assume append would return a reference to the list so that you could append multiple items at a time; you assume it returns a reference to the time being appended. Commented Sep 26, 2014 at 18:12

1 Answer 1

3

.append doesn't return anything - it modifies the list in place, so you're basically appending deep_copy(get_path_to_point(from_point)) to visited_with_path, which returns None, and then trying to .append(point) to None, which doesn't work.

Sign up to request clarification or add additional context in comments.

1 Comment

The fact that functions with no explicit return return None rather than raising some sort of continuity error has tripped up many a person when debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.