Browse by Topic

Popular

Latest

Latest Python Posts

Illustration of a Python dictionary with a key lookup using the get method returning a default value.
Python

Python get Method: Safe Dictionary Access

Learn how Python's dict.get() method provides safe dictionary access with default values, avoids KeyError, and improves code clarity.

Pythondictionaryget methodKeyError
Diagram showing attribute access interception by a descriptor object in Python.
Python

Python Descriptor Protocol: How to Control Attribute Access

Understand the Python descriptor protocol to control attribute access, implement validation, and build reusable attribute logic with __get__, __set__, and __delete__.

descriptorsattribute accessmetaprogrammingpython internals
Illustration of a Python descriptor protocol with a class attribute being intercepted by __get__ and __set__ methods.
Python

Python Descriptors Explained with Practical Examples

python descriptors: Understand the descriptor protocol in Python, how __get__, __set__, and __delete__ control attribute access, and when to use descriptors over @prop...

descriptorsmetaprogrammingpython-oopproperty
Diagram showing how a shallow copy shares nested objects while the top-level container is duplicated in Python.
Python

Python Shallow Copy: What Gets Copied and What Is Shared

Learn how Python's shallow copy differs from assignment and deep copy, where the copy boundary stops, and when each approach is appropriate in real code.

pythonshallow-copydeep-copycopy-module
Two variable name labels pointing to a single shared Python list object, with a separate copy arrow leading to an independent list.
Python

Python List Aliasing: Why Two Names Can Share One List

Understand how Python list aliasing works: assigning a list to a new name shares the same object, and shallow or deep copies can break the alias.

list aliasingpython listsshallow copydeep copy
Diagram showing a Python function with default arguments evaluated once at definition time, contrasting with call time.
Python

Python Default Argument Evaluation: The Once-Only Trap

python default argument evaluation: Understand why Python evaluates default arguments once at function definition, and how to avoid the mutable default trap.

default argumentsmutable defaultsfunction definitionPython gotchas
Diagram showing a Python function with a mutable default list being shared across multiple calls, illustrating the shared state problem.
Python

The Python Mutable Default Argument Problem

Learn why mutable default arguments in Python persist across calls and how the None sentinel pattern prevents shared-state bugs.

Pythonfunction defaultsmutable objectssentinel pattern
A Python function definition showing a mutable list default shared across multiple calls, illustrating the shared-state trap.
Python

Fixing the Python Mutable Default Argument Trap

Python evaluates mutable default arguments once, so a shared list or dict can leak state across calls. Learn the None-sentinel pattern and dataclass default_factory fix.

pythonmutable-default-argumentsfunction-signaturespython-dataclasses
Illustration of two Python objects forming a cycle with a garbage collector and weakref breaking the link.
Python

Python Cyclic References: How to Detect and Break Them

Learn how cyclic references form in Python, how the cyclic garbage collector reclaims them, and how to use weakref to avoid memory leaks in long-running code.

cyclic referencesgarbage collectionweakrefmemory management
Diagram showing a weak reference pointing to an object that can be garbage collected when no strong references remain.
Python

Python weakref: When and How to Use Weak References

Learn how Python's weakref module lets you observe objects without keeping them alive. See when to use weak references and how callbacks, WeakValueDictionary, and WeakKeyDictionary help manage object lifetimes.

weak referencesmemory managementgarbage collectionWeakValueDictionary