Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Reset State Using a Property Deleter

Learn how to use a property deleter to reset an object's internal cached/stateful data cleanly.

Python practice15 minMethods & PropertiesIntermediateLast updated April 7, 2026

Problem statement

Sometimes objects keep internal state (caches, counters, buffers) that you want to expose read-only but still allow consumers to reset. Python's @property decorator supports a deleter (@prop.deleter) which can be used to clear or reset internal state while keeping a clean property API. Implement a Counter class that: - Tracks an internal integer counter starting at 0. - Provides an increment(n=1) instance method that increases the counter by n and returns self to allow chaining. - Exposes the counter via a read-only property count. - Implements a property deleter that resets the counter to 0 when del obj.count is used. Also implement the top-level helper functions used by tests: - inc_and_get(n): create a Counter, increment it n times (using increment(n)), and return the current count. - inc_del_get(n): create a Counter, increment n times, delete the property (del counter.count), then return the count. - initial_count(): return the count of a newly created Counter. - large_increment(n): hidden test helper that increments by n and returns count. - del_before_increment(n): hidden test helper that deletes the count on a new instance, then increments n and returns count. Do not print; return values so the test harness can eval expressions.

Task

Implement a class with a property that exposes internal state and a deleter that resets that state; provide helper functions to exercise the behavior.

Examples

Basic usage

Input

c = Counter(); c.increment(3); c.count

Output

3

Incrementing 3 times yields a count of 3. The property count exposes the internal counter.

Resetting with the deleter

Input

c = Counter(); c.increment(5); del c.count; c.count

Output

0

Deleting the property using the deleter resets the internal counter to 0.

Input format

Each test calls one of the provided top-level helper functions, e.g. inc_and_get(3).

Output format

The expression returns a value (int) and the test harness compares its string representation.

Constraints

Use only core Python. The Counter.count property must be read-only (no setter). The deleter must reset internal state; increment must return self.

Samples

Sample 1

Input

inc_and_get(4)

Output

4

Creates a Counter, increments 4, returns 4.