We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Mridu Bhatnagar - Object Internals
Explore the internal workings of Python objects, including mutable and immutable types, shallow and deep copying, and integer caching, to gain a deeper understanding of the language's underlying mechanics.
- In Python, objects can be either mutable (changeable) or immutable (unchangeable).
- Mutable objects can be modified, while immutable objects cannot.
- In Python, objects have a type, value, and location (memory address).
- A shallow copy of an object is a new object that references the same embedded objects as the original.
- A shallow copy changes the original object when modifications are made, because they share the same memory references.
- Deep copy of an object is a new object that contains its own copies of the embedded objects.
- Deep copy does not share the same memory references as the original, so changes do not affect the original object.
- Mutable objects include lists, dictionaries, sets, and some custom objects.
- Immutable objects include integers, floats, booleans, strings, and some built-in types.
- Integer caching is used to optimize memory by reusing integers within the range of -5 to 256.
- Strings are interned, which means they are stored only once in memory, and all references to the same string are the same instance.
-
The
is
operator compares the memory addresses of two objects, while the==
operator compares the values. -
The
id()
function returns the memory address of an object. -
copy.copy()
is used to create a shallow copy, andcopy.deepcopy()
is used to create a deep copy of an object. -
sort()
andsorted()
are two different functions, with different uses and behaviors.