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

Related Questions

Can someone explain this weird behavior of python re? Obviously the string 'test' does
>> type countlines.py | python countlines.py Can someone explain the what type does? Does
The following is a python code using the subprocess module. Can someone explain what
Can someone explain what is happening here? class Test(object): __getitem__ = getattr t =
Can someone explain to me this odd thing: When in python shell I type
Can someone please explain algorithm for itertools.permutations routine in Python standard lib 2.6? I
Can someone explain what the dict class is used for? This snippet is from
I'm sure it's intentional, so can someone explain the rationale for this behavior: Python
Can someone explain to me why in python when we want to join a
Can someone explain the last line of this Python code snippet to me? Cell

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.