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

  • SEARCH
  • Home
  • 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 8439355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:59:22+00:00 2026-06-10T07:59:22+00:00

It seems that in Python, to declare a variable in a class, it is

  • 0

It seems that in Python, to declare a variable in a class, it is static (keeps its value in the next instances). What better way to get around this problem?

class Foo():
  number = 0
  def set(self):
    self.number = 1


>>> foo = Foo()
>>> foo.number
0
>>> foo.set()
>>> foo.number
1
>>> new_foo = Foo()
>>> new_foo.number
1
  • 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-10T07:59:24+00:00Added an answer on June 10, 2026 at 7:59 am

    Variables defined at the class level are indeed “static”, but I don’t think they work quite the way you think they do. There are 2 levels here which you need to worry about. There are attributes at the class level, and there are attributes at the instance level. Whenever you do self.attribute = ... inside a method, you’re setting an attribute at the instance level. Whenever python looks up an attribute, it first looks at the instance level, if it doesn’t find the attribute, it looks at the class level.

    This can be a little confusing (especially if the attribute is a reference to a mutable object). consider:

    class Foo(object):
        attr = []  #class level attribute is Mutable
        def __init__(self):
            # in the next line, self.attr references the class level attribute since
            # there is no instance level attribute (yet)
            self.attr.append('Hello')
            self.attr = []
            # Now, we've created an instance level attribute, so further appends will
            # append to the instance level attribute, not the class level attribute.
            self.attr.append('World')
    
    a = Foo()
    print (a.attr)  #['World']
    print (Foo.attr) #['Hello']
    b = Foo()
    print (b.attr)  #['World']
    print (Foo.attr) #['Hello', 'Hello']
    

    As others have mentioned, if you want an attribute to be specific to an instance, just initialize it as an instance attribute in __init__ (using self.attr = ...). __init__ is a special method which is run whenever a class is initialized (with a few exceptions that we won’t discuss here).

    e.g.

    class Foo(object):
       def __init__(self):
           self.attr = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When running svndumpfilter2 on Windows, I get a problem that seems to take its
Is it possible to declare a variable in Python, like so?: var so that
I am writing a python extension that seems to be leaking memory. I am
I am looking for a python SOAP 1.2 client but it seems that it
A question that seems to have quite a few options for Python, but none
There seems to be a plethora of documentation tools for Python. Another one that
It seems that Python has some limitations regarding instance methods. Instance methods can't be
When interacting with F# libraries from IronPython it seems that Python function object are
I'm implementing a Python ontology class that uses a database backend to store and
I'm trying to build my first facebook app, and it seems that the python

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.