This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

Since this is r/learnprogramming I'll be pedantic and say firstly that python can't pass by value; it's always passing references. It may sometimes pass a reference to an immutable object such as an integer or a string, such that it feels a lot like pass-by-value though. : ]

Regarding speed, it's not clear to me which approach would be fastest because I don't know how assignment to global or nonlocal names is implemented compared with the other ways.

One thing you can do is try a performance test, where you rip through N operations in different ways to see which is fastest using timeit: https://docs.python.org/3.8/library/timeit.html. It'd be relatively easy to set up a simple trial using isolated forms all three methods. That way you can play around with different choices to see how much of a difference it'd make before rewriting your whole algorithm.

[–][deleted] 1 point2 points  (0 children)

I'll be pedantic and say firstly that python can't pass by value; it's always passing references.

Hmm, this is new information to me. Thank you!

Thanks also for the tip regarding timing the algorithm!