Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
Overall
score
93%
A utility that tracks method calls on objects by wrapping methods to record their invocations without modifying the original class definition.
obj.method(1, 2, key='value'), it records {'args': (1, 2), 'kwargs': {'key': 'value'}} in obj.calls @testself just like the original method @testcalls list @test@generates
class TrackedMethod:
"""
A descriptor that wraps a method to track its calls while preserving method behavior.
Should track calls in a list accessible via the 'calls' attribute on the instance.
Each call record should be a dictionary with 'args' and 'kwargs' keys.
"""
def __init__(self, method):
"""Initialize with the method to track."""
pass
def __get__(self, instance, owner):
"""Implement descriptor protocol to return bound callable."""
pass
def track_method(obj, method_name):
"""
Replace a method on an object instance with a tracked version.
Args:
obj: The object instance to modify
method_name: Name of the method to track
Creates a 'calls' attribute on the instance (list of dicts) if not present.
"""
passThis implementation uses only Python standard library features and requires no external dependencies.
Install with Tessl CLI
npx tessl i tessl/pypi-wtfpythondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10