Hone logo
Hone
Problems

Mastering Static and Class Methods in Python

This challenge focuses on understanding and implementing static and class methods in Python. These methods offer distinct ways to associate behavior with a class rather than an instance, providing flexibility and control over how your code interacts with its data and structure. You'll build a Counter class that utilizes these methods to manage its counts in different ways.

Problem Description

You need to create a Python class named Counter that keeps track of a count. This Counter class should demonstrate the effective use of both static methods and class methods.

Key Requirements:

  1. Instance Method (increment): A regular instance method that increments the count attribute of the specific Counter object it's called on.
  2. Class Method (increment_global_count): A class method that increments a class-level counter. This class-level counter should be a class attribute shared by all instances of Counter.
  3. Static Method (get_description): A static method that returns a descriptive string about the Counter class itself. This method should not access or modify any instance or class attributes.
  4. Class Attribute for Global Count: The Counter class must have a class attribute to store the global count managed by the class method.

Expected Behavior:

  • When an instance of Counter is created, its individual count attribute should be initialized to 0.
  • Calling increment() on an instance should only affect that instance's count.
  • Calling increment_global_count() (either on the class or an instance) should update the shared class-level global count.
  • Calling get_description() should consistently return the same descriptive string, regardless of whether it's called on the class or an instance.

Edge Cases:

  • Ensure that creating multiple instances of Counter does not interfere with each other's instance counts, but they should all share the same global count.

Examples

Example 1:

Input:
counter1 = Counter()
counter2 = Counter()

counter1.increment()
counter1.increment()
counter2.increment()

Counter.increment_global_count()
Counter.increment_global_count()
counter1.increment_global_count() # Calling class method via instance

print(counter1.count)
print(counter2.count)
print(Counter.get_description())
print(Counter.global_count)

Output:
2
1
Counter class for tracking counts.
3

Explanation: counter1 was incremented twice, so counter1.count is 2. counter2 was incremented once, so counter2.count is 1. The global count was incremented a total of 3 times (2 via the class, 1 via an instance). get_description is a static method returning a fixed string.

Example 2:

Input:
counter3 = Counter()
counter3.increment()

Counter.increment_global_count()

print(counter3.count)
print(Counter.global_count)

Output:
1
1

Explanation: counter3 has its instance count incremented once. The global count is incremented once via the class.

Example 3:

Input:
print(Counter.get_description())

Output:
Counter class for tracking counts.

Explanation: This demonstrates calling the static method directly on the class, showing it doesn't depend on instance or class state.

Constraints

  • The Counter class must be named exactly Counter.
  • The class attribute for the global count must be named global_count.
  • The instance method must be named increment.
  • The class method must be named increment_global_count.
  • The static method must be named get_description.
  • The individual instance count should be stored in an attribute named count.

Notes

  • Remember the @classmethod and @staticmethod decorators.
  • Consider how self and cls are passed to different types of methods.
  • The global_count should be initialized as a class attribute.
Loading editor...
python