TGViewer
Python Learning Python Learning @python_bds · 5.75K subscribers
Post #950 1.08K
Feels off?

It's because all your objects share one variable (without you realizing it)

At first glance, it seems like every object should start fresh, right? But in this case, count is a class variable, which means it’s shared by all instances of the class.

Every time you create a new Counter(), you’re actually incrementing the same shared variable not something unique to each object.

If your goal is to give each object its own value, define it like this instead

class Counter:
    def __init__(self):
        self.count = 1

Now, each instance has its own count, stored on the object itself . no sharing, no surprises.
  • ❤ 3
More from @python_bds
  1. Oct 11, 2026📘 The Big Book of Small Python Projects ✍️ Author: Al Sweigart 🔗 Read Online #Python #Pr…
  2. Oct 10, 2026document post
  3. Oct 10, 2026This Book Explains more than 400 Concepts with codes.
  4. Oct 8, 2026🐛 This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  5. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  6. Oct 4, 2026Python Set Methods ✍️
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →