Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:55+00:00 2026-06-18T03:31:55+00:00

I ran into a weird problem today, here is some example code from collections

  • 0

I ran into a weird problem today, here is some example code

from collections import defaultdict

class Counter:
    hits = 0
    visitors = set()

    def addHit(self, ip):
        self.hits += 1
        self.visitors.add(ip)

d = defaultdict(Counter)
d['a'].addHit('1.1.1')
d['a'].addHit('2.2.2')
d['b'].addHit('3.3.3')

print d['a'].hits, d['a'].visitors
print d['b'].hits, d['b'].visitors

Expected Result:

2 set(['1.1.1', '2.2.2'])
1 set(['3.3.3'])

Actual Result:

2 set(['1.1.1', '3.3.3', '2.2.2'])
1 set(['1.1.1', '3.3.3', '2.2.2'])

Why are the visitor sets sharing data between what I thought should be separate instances of the Counter class. Shouldn’t each input point to a specific instance?

What makes this more difficult to understand is that the hit counter seems to work fine and keep things separate.

Can anyone help me understand what’s going on here or how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:31:56+00:00Added an answer on June 18, 2026 at 3:31 am

    I suspect your visitors set is a class variable and not an instance variable.

    Nothing to do with defaultdicts behaviour.

    Try:

    class Counter:
        def __init__(self):
            self.hits = 0
            self.visitors = set()
    
        def addHit(self, ip):
            self.hits += 1
            self.visitors.add(ip)
    

    EDIT:
    Nothing to do with your questions, but just some ideas how to expand your counter:

    #! /usr/bin/python3.2
    
    class Counter:
        def __init__(self):
            self.__hits = 0
            self.__visitors = {}
    
        def addHit(self, ip):
            self.__hits += 1
            if ip not in self.__visitors:
                self.__visitors [ip] = 0
            self.__visitors [ip] += 1
    
        @property
        def hits (self):
            return self.__hits
    
        @property
        def uniqueHits (self):
            return len (self.__visitors)
    
        @property
        def ips (self):
            return (ip for ip in self.__visitors)
    
        def __getitem__ (self, ip):
            return 0 if ip not in self.__visitors else self.__visitors [ip]
    
    c = Counter ()
    
    c.addHit ('1.1.1.1')
    c.addHit ('1.1.1.1')
    c.addHit ('1.1.1.1')
    c.addHit ('1.1.1.1')
    c.addHit ('1.1.1.2')
    c.addHit ('1.1.1.2')
    c.addHit ('1.1.1.3')
    
    print (c.hits)
    print (c.uniqueHits)
    for ip in c.ips:
        print (ip, c [ip] )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this very weird problem when coding up some CUDA code: the
Today I ran into some weird behaviour with a HashSet's iterator. In the code
Today, I ran into this weird problem with a user using Mac OS X.
I ran into a very weird problem today. Long story short, my function returns
Today I ran into a weird problem. I have a database in SQL Server
i ran into this wierd problem: the $myImg variable has been extracted from some
So I ran into a situation today where some production code was failing precisely
I ran into a weird problem today that I don't really fully understand. Hopefully
Ran into a weird problem with RavenDB public ActionResult Save(RandomModel model) { //Do some
Today I ran into something a bit weird. I used mini-profiler to see the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.