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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:59:25+00:00 2026-05-15T08:59:25+00:00

Can someone explain why Python does the following? >>> class Foo(object): … bar =

  • 0

Can someone explain why Python does the following?

>>> class Foo(object):
...   bar = []
...
>>> a = Foo()
>>> b = Foo()
>>> a.bar.append(1)
>>> b.bar
[1]
>>> a.bar = 1
>>> a.bar
1
>>> b.bar
[1]
>>> a.bar = []
>>> a.bar
[]
>>> b.bar
[1]
>>> del a.bar
>>> a.bar
[1]

It’s rather confusing!

  • 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-15T08:59:25+00:00Added an answer on May 15, 2026 at 8:59 am

    As others have said the code as written creates a class variable rather than an instance variable. You need to assign in __init__ to create an instance variable.

    Hopefully this annotated copy of your code is helpful in explaining what’s going on at each stage:

    >>> class Foo(object):
    ...   bar = []          # defines a class variable on Foo (shared by all instances)
    ...
    >>> a = Foo()
    >>> b = Foo()
    >>> a.bar.append(1)     # appends the value 1 to the previously empty list Foo.bar
    >>> b.bar               # returns the value of the class variable Foo.bar
    [1]
    >>> a.bar = 1           # binds 1 to the instance variable a.bar, masking the access
    >>> a.bar               # you previously had to the class variable through a.bar
    1
    >>> b.bar               # b doesn't have an instance variable 'bar' so this still
    [1]                     # returns the class variable
    >>> a.bar = []          # bind a's instance variable to to an empty list
    >>> a.bar
    []
    >>> b.bar               # b doesn't have an instance variable 'bar' so this still
    [1]                     # returns the class variable
    >>> del a.bar           # unbinds a's instance variable unmasking the class variable
    >>> a.bar               # so a.bar now returns the list with 1 in it.
    [1]
    

    Also, printing out the value of Foo.bar (the class variable accessed via the class rather than via an instance) after each of your statements might help clarify what is going on.

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

Sidebar

Ask A Question

Stats

  • Questions 531k
  • Answers 531k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Read from the console using Console.ReadLine() (which requires a carriage… May 16, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer What you probably want to override is layoutSubviews rather than… May 16, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer Serialization is not the answer. What you want to do… May 16, 2026 at 11:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Can someone explain what is happening here? class Test(object): __getitem__ = getattr t =
Can someone explain how egg-info directories are tied to their respective modules? For example,
Can someone explain how MapReduce works with Cassandra .6? I've read through the word
Can someone explain to me how exactly the NSTimer behavior is? Basically I want
Can someone explain why this code doesn't work as expected? I would expect it
Can someone explain ProxyFactoryBean in simple terms? I see this being quoted lot of
New here. Also I'm (very) new to python and trying to understand the following
I've just upgraded from Python 2.6.1 to 2.6.4 on my development machine and upon
I recently began learning Python, and I am a bit confused about how packages
There is this site ... ... where they explain how to draw an antialiased

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.