python 参数传递

参数传递的方式主要有两种:

  1. 值传递
  2. 引用传递

python 属于哪一种? 严格来讲,两种都不是,但是也是可以归纳总结的:

Argument Passing Summary Argument passing in Python can be summarized as follows.

  • Passing an immutable object, like an int, str, tuple, or frozenset, to a Python function acts like pass-by-value. The function can’t modify the object in the calling environment.
  • Passing a mutable object such as a list, dict, or set acts somewhat—but not exactly—like pass-by-reference. The function can’t reassign the object wholesale, but it can change items in place within the object, and these changes will be reflected in the calling environment.