Browse by Topic

Popular

Latest

Latest Posts

Illustration comparing a nominal inheritance hierarchy with a structural protocol contract in Python typing.
Python

Python Structural Typing vs Nominal Typing

Understand the difference between Python's nominal and structural typing, how Protocol declares structural contracts, and when @runtime_checkable makes isinstance checks work.

typingProtocoltype-checkingduck-typing
Illustration of a Python class with __slots__ showing reduced memory footprint and faster attribute access.
Python

Python __slots__: Advantages and Disadvantages

Understand how Python `__slots__` reduces memory and can speed up attribute access, plus the constraints and inheritance rules it introduces.

pythonperformancememory optimizationobject-oriented programming
Diagram comparing a Python function with a callable object instance that holds state.
Python

Python Callable Object vs Function: Key Differences

python callable object vs function: Understand the difference between Python callable objects and functions, when each fits, and how state and closures change the choice.

callable objects__call__ methodpython functionsclosures
Diagram comparing Python metaclass and __init_subclass__ hooks during class creation
Python

Python Metaclass vs __init_subclass__: When to Use Each

Compare Python metaclasses and __init_subclass__ for controlling class creation. Learn when each approach fits, how they differ, and how to combine them.

PythonMetaclass__init_subclass__Class Creation
Illustration comparing Python descriptor and decorator concepts
Python

Python Descriptor vs Decorator: What's the Difference?

Understand the difference between Python descriptors and decorators, how they work, and when to use each in your code.

descriptorsdecoratorspythonmetaprogramming
Diagram comparing Python's getattr and getattribute attribute lookup methods
Python

Python getattr vs getattribute: Key Differences

Understand when Python calls __getattr__ vs __getattribute__, how to override each safely, and how to avoid recursion and performance problems.

PythonAttribute Accessgetattrgetattribute
Illustration of two Python objects with matching equality and hash symbols representing the consistency requirement.
Python

Python __eq__ vs __hash__: The Contract You Must Follow

Understand how Python's __eq__ and __hash__ methods interact, why they must stay consistent, and how to implement them correctly for set membership and dictionary keys.

pythondunder methodshashableobject equality
Illustration of Python comparison operators on custom objects, showing equality and ordering symbols.
Python

Python Rich Comparison Methods Explained

Learn how to implement Python rich comparison methods for custom classes, avoid common pitfalls, and keep your code maintainable.

comparison operatorsoperator overloadingtotal_orderinghash contract
Diagram showing a Python list of objects on the left, each with a highlighted field, an arrow representing the key function extracting that field into a separate list, and a sorted list on the right.
Python

Python Comparison Key Function Explained

Learn how Python's comparison key function works with sorting, min(), and max(), including examples using lambda, itemgetter, and attrgetter.

pythonsortingkey-functionlambda
Comparison of Python sorted key and comparator approaches with a visual representation of sorting logic.
Python

python sorted key vs comparator

Compare Python's sorted key parameter with comparator functions via functools.cmp_to_key(). Learn when to use each, the performance tradeoffs, and how to avoid common sorting mistakes.

sortingkey functioncomparatorcmp_to_key