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 6470441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:06:42+00:00 2026-05-25T06:06:42+00:00

This is an easy way to earn some points. Please explain the following: class

  • 0

This is an easy way to earn some points. Please explain the following:

class C:
    a = {}
    b = 0
    c = []

    def __init__(self):
        self.x = {}

    def d(self, k, v):
        self.x[k] = v
        self.a[k] = v;
        self.b = v
        self.c.append(v)

    def out(self, k):
        print(self.x[k], self.a[k], self.b, self.c[0])

c = C()
d = C()
c.d(1, 10)
d.d(1, 20)
c.out(1)  
d.out(1)

Will output the following:

10 20 10 10
20 20 20 10

Why does a dictionary, a list and ‘plain’ variable each behave differently?

Edit: I was thinking the question is obvious but let me spell it in greater detail:

I have a class with three attributes, a, b, and c. I create two instances of the class. I then call a method that modifies these attributes for each instance. When I inspect the attributes, I find that if an attribute is a dictionary, it is shared across all instances, while if it is a ‘plain’ variable, it behaves as one would expect, being different for each instance.

  • 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-05-25T06:06:43+00:00Added an answer on May 25, 2026 at 6:06 am

    First of all, [] is not an array, it’s a list. The issue here is how the attribute resolution and mutable variables work. Let’s start with

    class Foo(object):
        a = {}
        b = 0
        c = []
    

    This creates a class with three attributes — those are available either through class itself (Foo.a, for example), or through class’ instance (Foo().a). Attributes are stored in a special thingy called __dict__. Both class and an instance have one (there are cases in which this is not true, but they’re irrelevant here) — but in the case of Foo, the instance __dict__ is empty when the instance is created — so when you do Foo().a, in reality you’re accessing the same object as in Foo.a.

    Now, you’re adding __init__.

    class Foo(object):
        # ...
    
        def __init__(self):
            self.x = {}
    

    This creates an attribute not in the class’ __dict__, but in the instance one, so you cannot access Foo.x, only Foo().x. This also means x is a whole different object in every instance, whereas class attributes are shared by all of the class instances.

    Now you’re adding your mutation method.

    class Foo(object):
        # ...
    
        def mutate(self, key, value):
            self.x[key] = value
            self.a[key] = value
            self.b      = value
            self.c.append(value)
    

    Do you recall that self.x = {} creates an instance attribute? Here self.b = value does the same exact thing — it doesn’t touch the class attribute at all, it creates a new one that for instances overshadows the shared one (that’s how references work in Python — assignment binds the name to an object, and never modifies the object that the name was pointing to).

    But you don’t rebind self.a and self.c — you mutate them in-place (because they’re mutable and you can do that) — so in fact you’re modifying the original class attributes, that’s why you can observe the change in the other instance (as those are shared by them). self.x behave differently, because it’s not a class attribute, but rather an instance one.

    You also print only first element of self.c — if you’d print all of it, you’d see it’s [10, 20].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an easy way to draw some text with a default font. pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
Is there an easy way to figure this out? I guess I can use
Is there a easy way to do this? Or do I have to parse
In there an easy way to do this in PHP. I want to make
Is there an easy way to modify this code so that the target URL
Is there an easy way to convert a string that contains this: Date: Wed,
There must be an easy way to do this, but somehow I can wrap
There must be an easy way to do this. I build a Flex app
There gotta be an easy way to do this, I can't believe there's none.
Is there an easy way to convert date and time like this: Sun, 14

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.