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

The consequences of immutability


Python has two broad flavors of objects: mutable and immutable. A mutable object has an internal state that can be updated by using operators or method functions. An immutable object's state cannot be changed.

The canonical examples of immutable objects are the numbers. The number 2 must always have a single, immutable value midway between 1 and 3. We can't change the state of 2 to make it 3 without making a mockery of the idea of mathematical truth.

In Chapter 6, More Complex Data Types, we'll look at a number of mutable data structures. The most important three mutable collections are set, list, and dict. These objects can have items added, and removed; we can change the state of the object.

In addition to numbers being immutable, three other common structures are also immutable: str, bytes, and tuple. Because strings and bytes are immutable, the string manipulation methods will always create a new string object from one or more existing string objects.

This...