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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:31:09+00:00 2026-05-26T09:31:09+00:00

I have a Python class that has a class attribute set to something other

  • 0

I have a Python class that has a class attribute set to something other than None. When creating a new instance, the changes made to that attribute perpetuates through all instances.

Here’s some code to make sense of this:

 class Foo(object):
   a = []
   b = 2

 foo = Foo()
 foo.a.append('item')
 foo.b = 5

Using foo.a returns ['item'] and foo.b returns 5, as one would expect.

When I create a new instance (we’ll call it bar), using bar.a returns ['item'] and bar.b return 5, too! However, when I initially set all the class attributes to None then set them to whatever in __init__, like so:

 class Foo(object):
   a = None
   b = None

   def __init__(self):
     self.a = []
     self.b = 2

Using bar.a returns [] and bar.b returns 2 while foo.a returns ['item'] and foo.b returns 5.

Is this how it’s suppose to work? I’ve apparently never ran into this issue in the 3 years I’ve programmed Python and would like some clarification. I also can’t find it anywhere in the documentation, so giving me a reference would be wonderful if possible. 🙂

  • 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-26T09:31:10+00:00Added an answer on May 26, 2026 at 9:31 am

    Yes, this is how it is supposed to work.

    If a and b belong to the instance of Foo, then the correct way to do this is:

    class Foo(object):
       def __init__(self):
         self.a = []
         self.b = 2
    

    The following makes a and b belong to the class itself, so all instances share the same variables:

    class Foo(object):
       a = []
       b = 2
    

    When you mix the two methods — as you did in your second example — this doesn’t add anything useful, and just causes confusion.

    One caveat worth mentioning is that when you do the following in your first example:

    foo.b = 5
    

    you are not changing Foo.b, you are adding a brand new attribute to foo that “shadows” Foo.b. When you do this, neither bar.b nor Foo.b change. If you subsequently do del foo.b, that’ll delete that attribute and foo.b will once again refer to Foo.b.

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

Sidebar

Related Questions

I have something like that in my python code class A: __mess = Yeap!
I have a simple Invoices class with a Number attribute that has to be
I'm new to python, this is a class that I have class Goal: def
Suppose I have a Python class that I want to add an extra property
If I have a python class, how can I alias that class-name into another
I have written a class in python that implements __str__(self) but when I use
I have a python module that defines a number of classes: class A(object): def
I'm trying to write a Python class that has the ability to do the
I have a python class hierarchy, that I want to extend at runtime. Furthermore
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel

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.