Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compare Instance Method and Static Method Behavior

Explore differences between instance methods and static methods: how they access instance data, and how they're called.

Python practice16 minMethods & PropertiesIntermediateLast updated April 7, 2026

Problem statement

Static methods and instance methods serve different purposes: - Instance methods receive the instance (self) and can access instance attributes. - Static methods do not receive an implicit instance and behave like plain functions attached to the class namespace. Implement a class Adder that: - Is initialized with an integer offset stored on the instance. - Has an instance method add(self, x) that returns offset + x. - Has a static method add_static(x, y) that returns x + y. Also implement top-level helper functions used by tests: - instance_add(offset, x): returns Adder(offset).add(x) - static_add(x, y): returns Adder.add_static(x, y) - static_via_instance(offset, x, y): calls the static method via an instance (Adder(offset).add_static(x, y)) and returns the result - try_call_instance_on_class(x): hidden helper that attempts to call the instance method on the class (Adder.add(x)) and returns the string 'TypeError' if a TypeError occurs, otherwise returns the value. - static_ignores_instance(offset, x, y): hidden helper that demonstrates the static method's independence of instance state by calling it via an instance with a large offset and returning the result. Design your helpers to return values (or strings for exceptions) so tests can eval them.

Task

Implement a class with an instance method and a static method, and provide helpers that demonstrate how each behaves when called from the class or an instance.

Examples

Instance method uses instance offset

Input

Adder(5).add(2)

Output

7

Instance method add accesses self.offset and returns offset + x.

Static method does not need an instance

Input

Adder.add_static(3, 4)

Output

7

Static methods behave like plain functions attached to the class; they don't receive self.

Input format

Each test calls one of the provided helper functions, e.g. instance_add(3, 4) or static_add(3, 4).

Output format

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

Constraints

Use only core Python. The instance method must use the instance's stored offset. Static method must not use instance state.

Samples

Sample 1

Input

instance_add(3, 4)

Output

7

Creates Adder with offset 3 and adds 4 -> 7