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

  • Home
  • SEARCH
  • 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 8727385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:25:35+00:00 2026-06-13T08:25:35+00:00

Why does set behave differently from a dictionary for class variables in python. For

  • 0

Why does set behave differently from a dictionary for class variables in python. For instance,

class Test1:
   x=set()
   y={}

hamster=Test1()
chinchilla=Test1()
hamster.x.add('hi')  # now both sets in both instances have 'hi'
hamster.y['key']=5   # only the hamster instance will contain 5

Thanks for any help 🙂

EDIT: I also noticed that if you define self.x=set() in init(), you can avoid the issue of adding to both instances. Removed typos

  • 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-13T08:25:36+00:00Added an answer on June 13, 2026 at 8:25 am

    No you’re wrong both have the key:5:

    In [56]: class Test1:
       ....:        x=set()
       ....:    y={}
       ....: 
    
    In [57]: hamster=Test1()
    
    In [58]: chinchilla=Test1()
    
    In [59]: hamster.x.add('hi')  # now both sets in both instances have 'hi'
    
    In [60]: hamster.y['key']=5
    
    In [62]: hamster.x,chinchilla.x
    Out[62]: (set(['hi']), set(['hi']))
    
    In [63]: hamster.y,chinchilla.y
    Out[63]: ({'key': 5}, {'key': 5})
    

    Actually in your code you’re not changing instance variable, you’re changing class variables:

    In [65]: Test1.x
    Out[65]: set(['hi'])
    
    In [66]: Test1.y
    Out[66]: {'key': 5}
    

    you need to use instance variables here:

    In [71]: class Test1():
        def __init__(self):
            self.x=set()
            self.y={}
       ....:         
       ....:         
    
    In [75]: hamster=Test1()
    
    In [76]: chinchilla=Test1()
    
    In [77]: hamster.x.add('hi')
    
    In [78]: chinchilla.x.add('bye')
    
    In [79]: hamster.x
    Out[79]: set(['hi'])
    
    In [81]: chinchilla.x
    Out[81]: set(['bye'])
    
    
    In [82]: hamster.y['key']=5
    
    In [83]: hamster.y,chinchilla.y
    Out[83]: ({'key': 5}, {})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does ERRORLEVEL behave differently in these two circumstances? From the command line: Microsoft
Does SET IDENTITY_INSERT [Table] ON persist beyond the scope of a SQL Script? I'm
Does set,bag while cascading is used for Ilist or Iset only. What if the
Does it set a flag in a bean ? Does it load special bean
How does one set the date on the CalendarDatePicker. i.e. it defaults to current
SetThreadName does not set thread name with Visual Studio 2005, when used as below:
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
<Keyboard> does not set the required layout_height attribute: (1) Set to wrap_content (2) Set
Does anyone know how to set the day of the week when a date
Does anyone know how to set the TextBox.Width property to fill up the remainder

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.