The self Keyword

You already used self in __init__. The next step is using it correctly in every other instance method too. Without self., you create a local variable instead of changing the object itself.

self in Any Instance Method

class Dog:
    def bark(self):
        print(f"{self} says Woof!")

d = Dog()
d.bark()       # Python calls: Dog.bark(d)
Dog.bark(d)    # identical — self IS the object d

Both lines do the same thing. d.bark() is just shorthand — Python fills in self with d automatically.

Instance Variable vs Local Variable

class Counter:
    def __init__(self):
        self.count = 0      # instance variable — lives on the object

    def bad_increment(self):
        count = count + 1   # local variable — dies when method ends

    def good_increment(self):
        self.count += 1     # instance variable — persists

Forgetting self. is one of the most common Python OOP bugs. The local variable is completely separate from the instance variable — and it will cause an UnboundLocalError if you try to read it before assigning.

Instance Isolation

Because each object has its own set of instance variables, two Counterobjects can count independently:

c1 = Counter()
c2 = Counter()
c1.count += 5
print(c1.count)  # → 5
print(c2.count)  # → 0 (completely unaffected)

Your Task

The Counter class is a debugging exercise. Each method uses count without self., so the value is not stored on the object. Fix all three:

  1. increment: change count += 1 to self.count += 1
  2. reset: change count = 0 to self.count = 0
  3. get_count: change return count to return self.count
Section 1/4self in Any Instance Method

self in Any Instance Method

Both lines do the same thing. d.bark() is just shorthand — Python fills in self with d automatically.

The self Keyword

You already used self in __init__. The next step is using it correctly in every other instance method too. Without self., you create a local variable instead of changing the object itself.

self in Any Instance Method

class Dog:
    def bark(self):
        print(f"{self} says Woof!")

d = Dog()
d.bark()       # Python calls: Dog.bark(d)
Dog.bark(d)    # identical — self IS the object d

Both lines do the same thing. d.bark() is just shorthand — Python fills in self with d automatically.

Output:

Click "Check" to run your code.