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

The Archive Base Latest Questions

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

I learned that Python class attributes are like static data members in C++. However,

  • 0

I learned that Python class attributes are like static data members in C++. However, I got confused after trying the following code:

>>> class Foo:
...     a=1
... 
>>> f1=Foo();
>>> f2=Foo()
>>> f1.a
1
>>> f1.a=5
>>> f1.a
5
>>> f2.a
1

Shouldn’t f2.a also equal 5?

If a is defined as a list instead of an integer, the behavior is expected:

>>> class Foo:
...     a=[]
... 
>>> f1=Foo();
>>> f2=Foo()
>>> f1.a
[]
>>> f1.a.append(5)
>>> f1.a
[5]
>>> f2.a
[5]

I looked at
What is the difference between class and instance attributes?, but it doesn’t answer my question.

Can anyone explain why the difference?

  • 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-25T22:59:04+00:00Added an answer on May 25, 2026 at 10:59 pm

    You’re not doing the same thing in your second example. In you first example, you are assigning f1.a a new value:

    f1.a = 5
    

    In your second example, you are simply extending a list:

    f1.a.append(5)
    

    This doesn’t change what f1.a is pointing to. If you were instead to do this:

    f1.a = [5]
    

    You would find that this behaves the same as your first example.

    But consider this example:

    >>> f1=Foo()
    >>> f2=Foo()
    >>> Foo.a = 5
    >>> f1.a
    5
    >>> f2.a
    5
    

    In this example, we’re actually changing the value of the class attribute,
    and the change is visible in all instances of the class. When you
    type:

    f1.a = 5
    

    You’re overriding the class attribute with an instance attribute.

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

Sidebar

Related Questions

I like using Python, because of the easy-to-learn syntax, however, I recently learned it
I learned that when executing commands in Python, I should use subprocess. What I'm
First, let me say that I'm a complete beginner at Python. I've never learned
I learned that by trying to use the tablesorter plug in from jquery the
I've learned that static scoping is the only sane way to do things, and
So I have learned that that the Microsoft.Jet.OLEDB.4.0 data provider for querying data sources
When I first learned Python, I got used to doing this: print text, lineNumber,
After I learned about reading unicode files in Python 3.0 web script, now it's
In python (and some other languages) I have learned, that the name of a
I've recently learned that python doesn't have the switch/case statement. I've been reading about

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.