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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:43:53+00:00 2026-06-03T18:43:53+00:00

Here’s a bit of a newbie Python question about instance variables. Consider the following

  • 0

Here’s a bit of a newbie Python question about instance variables.

Consider the following Python 2.7 class definition:

class Foo(object):
    a = 1

    def __init__(self):
            self.b = 2

    def __repr__(self):
            return "%s" % self.__dict__

Now, when I create an instance of Foo, Foo.__dict__ contains b, but not a.

>>> x=Foo()
>>> x
{'b': 2}
>>> dir(x)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', 
 '__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
 '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
>>> x.__dict__
{'b': 2}

And here I thought I had a pretty good grasp on the Way of the Python.

What’s the difference between x.a and x.b? As far as I can tell they’re both instance variables.

Edit: OK, re-reading the Python docs I see that Foo.a is a class attribute rather than an instance variable. Hm… I guess the confusion comes from the fact that I can assign a new value to x.a and the new value only affects the x instance — I suppose I’m now aliasing a member variable over the top of the Foo.a attribute:

>>> y=Foo()
>>> y.a = 2
>>> y
{'a': 2, 'b': 2}
>>> x
{'b': 2}
>>> x.a
1
>>> z=Foo()
>>> z
{'b': 2}
>>> z.a
1
>>> Foo.a
1
>>> x.a
1
>>> y.a
2

So, now I overwrite the previous value of Foo.a, and it affects all instances of Foo that haven’t aliased Foo.a:

>>> Foo.a=999
>>> x.a
999
>>> y.a
2
  • 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-06-03T18:43:56+00:00Added an answer on June 3, 2026 at 6:43 pm

    Your a isn’t an instance variable. You defined it as part of the class.

    >>> class Foo(object):
    ...    a = 1
    ...
    >>> Foo.a
    1
    

    If you want an instance variable you should put it inside the __init__ method, because this method is called when your object is created.

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

Sidebar

Related Questions

Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
Here's my scenario: <!-- Normal Control --> <div class=required> <label for=address1>Address line 1</label> <input
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here a simple question : What do you think of code which use try
Here is my question. I am having this simple menu. <div id=menu> <ul> <li>
Here is my code: echo '<table class=class1><tbody>'; while ($row1=mysql_fetch_array($result1)){ echo '<tr><td>'.$row1['firstname'].'</td><td>'.$row1['lastname'].'</td></tr>'; } echo '</tbody></table>';
Here is what I want: <h3 class=ExpressCheckoutTitle> <a onclick= href=# class=ChangeLink>Modify »</a> Step 4:
Here is some interesting question .I am developing an app with tableview. Initially tableview
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here the total height of all <div> 's are 900 pixels, but the jQuery

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.