or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wtfpython@3.0.x
tile.json

tessl/pypi-wtfpython

tessl install tessl/pypi-wtfpython@3.0.0

Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.06x

Baseline

Agent success rate without this tile

88%

task.mdevals/scenario-8/

Class Behavior Analyzer

Build a Python utility that helps developers understand how Python handles class attributes, method identity, and name mangling.

Requirements

Create a module that implements the following capabilities:

1. Attribute Type Checker

Implement a function that determines whether an attribute belongs to the class or to an instance:

  • Accept a class, an instance, and an attribute name
  • Check if the attribute exists in the class namespace or instance namespace
  • Return whether it's a class attribute, instance attribute, both, or neither

2. Method Identity Comparator

Implement a function that demonstrates method identity behavior:

  • Accept an object and a method name
  • Retrieve the method twice from the same object
  • Compare the two references using identity (is) and equality (==)
  • Return the results of both comparisons

3. Name Mangler

Implement a function that handles Python's name mangling for private attributes:

  • Accept a class name and an attribute name
  • If the attribute is private (starts with __ but doesn't end with __), return the mangled name
  • Otherwise, return the original name
  • Follow Python's name mangling rules: _ClassName__attributename

Test Cases

  • Given an attribute that exists only on the class, check_attribute_type returns 'class' @test
  • Given an attribute that exists only on an instance, check_attribute_type returns 'instance' @test
  • When comparing two method references from the same object using compare_method_identity, the identity check returns False but equality check returns True @test
  • Given a private attribute name __secret, get_mangled_name('MyClass', '__secret') returns _MyClass__secret @test

Implementation

@generates

API

def check_attribute_type(cls, obj, attr_name):
    """
    Determines whether an attribute is a class attribute, instance attribute, or both.

    Args:
        cls: The class to check
        obj: An instance of the class
        attr_name: The name of the attribute to check

    Returns:
        A string: 'class', 'instance', 'both', or 'neither'
    """
    pass

def compare_method_identity(obj, method_name):
    """
    Compares the identity and equality of method references from the same object.

    Args:
        obj: An object with methods
        method_name: The name of the method to check

    Returns:
        A dict with keys 'is_identical' (bool) and 'is_equal' (bool)
    """
    pass

def get_mangled_name(class_name, attr_name):
    """
    Returns the mangled version of a private attribute name.

    Args:
        class_name: The name of the class
        attr_name: The attribute name (may start with __)

    Returns:
        The mangled attribute name if applicable, or the original name
    """
    pass

Dependencies { .dependencies }

wtfpython { .dependency }

Provides educational examples of Python's class and object behavior patterns, including attribute handling, method identity, and name mangling.