Variables, references, and memory model

Variables, references, and memory model

This section covers the core concepts of Python, including language fundamentals, built-in data types, and variables. Understanding these basics is crucial for anyone looking to build a solid foundation in Python programming.

11 audio · 2:51

Nortren·

How does variable assignment work in Python?

0:13
In Python, variables are names bound to objects in memory. Assignment does not copy values; it binds a name to the same object. When you write a equals b, both names refer to the exact same object until one of them is reassigned.

What is the difference between is and equals in Python?

0:14
The double-equals operator compares values for equality and calls the dunder eq method. The is operator compares object identity, that is, whether two names refer to the same object in memory. Two distinct lists with the same contents are equal but not identical.

What is object identity?

0:13
Object identity is a unique integer that identifies an object during its lifetime in memory. You can retrieve it with the id function. In CPython, identity is the memory address of the object. Two variables refer to the same object if and only if their identities are equal.

What is small integer caching in Python?

0:16
CPython caches small integers in the range from negative five to two hundred fifty six. Two variables assigned to the integer 100 will be the same object in memory, while two variables assigned to 1000 may not be. This is an implementation detail and should never be relied upon for program logic.

What is string interning?

0:17
String interning is an optimization where Python keeps a single copy of certain strings in memory and reuses them. Short strings that look like identifiers are typically interned. You can force interning with the sys.intern function. Interned strings can be compared by identity, which is faster than character comparison.

What is the difference between mutable and immutable objects?

0:17
Immutable objects cannot be changed after creation. Examples include int, float, str, tuple, frozenset, and bytes. Mutable objects can be changed in place. Examples include list, dict, set, and bytearray. Mutability affects how objects behave when shared between variables and passed to functions.

Why does Python use mutable default arguments cause bugs?

0:18
Default arguments are evaluated only once, when the function is defined, not on each call. If the default is a mutable object like an empty list, all calls share the same list. Modifications persist across calls, causing surprising behavior. The fix is to use None as the default and create a fresh list inside the function.

How does Python pass arguments to functions?

0:16
Python uses pass-by-object-reference, sometimes called pass-by-assignment. The function receives a reference to the same object the caller has. Reassigning the parameter inside the function does not affect the caller. Mutating the object in place does affect the caller, because both names reference the same object.

What is the difference between shallow copy and deep copy?

0:17
A shallow copy creates a new container but reuses references to the inner objects. A deep copy recursively copies all nested objects. The copy module provides copy.copy for shallow and copy.deepcopy for deep copies. Shallow copies are faster but can lead to bugs when nested mutable objects are unintentionally shared.

What is reference counting in Python?

0:14
Reference counting is the primary memory management mechanism in CPython. Every object has an internal counter of how many references point to it. When the count drops to zero, the object is immediately deallocated. This makes most memory management deterministic and predictable.

How does Python handle reference cycles?

0:16
Reference counting alone cannot collect cycles, where objects reference each other in a loop. Python has a generational garbage collector that periodically detects and collects unreachable cycles. It is provided by the gc module and divides objects into three generations based on their survival history. ---