Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mutable and immutable argument values


In some programming languages, there are multiple function evaluation strategies, including call-by-value and call-by-reference. In call-by-value semantics, copies of argument values are assigned to the parameter variables in a function. In call-by-reference semantics, a reference to a variable is used in the function. This means that an assignment statement inside a function could replace the value of a variable outside the function. Neither of these types of semantics apply to Python.

Python uses a mechanism named "call-by-sharing" or "call-by-object". A function is given a reference to the original object. If that object is mutable, the function can mutate the object. The function cannot, however, assign to variables outside the function via the parameter variables. The function shares the objects, not the variables to which the objects are assigned.

One of the most important consequences is that the body of a function can assign new values to parameter...