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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:11:12+00:00 2026-06-14T08:11:12+00:00

This plagued me for hours, as I am from the C++ world. I finally

  • 0

This plagued me for hours, as I am from the C++ world. I finally found out what was going on, but I do not know why this is the default behaviour. I’d like to understand why the language is designed this way.

I wanted an instance variable mem. So I tried this:

class x(object):
   mem = []

obj = x()
obj.mem.append(1)
print(obj.mem)
objTWO = x()
objTWO.mem.append(2)
print(objTWO.mem)

Which prints this:

[1] 

[1, 2]

Whereas:

class x(object):

    def __init__(self):
        self.mem = []

obj = x()
obj.mem.append(1)
print(obj.mem)
objTWO = x()
objTWO.mem.append(2)
print(objTWO.mem)

prints

[1]

[2]

Why would the first be the default behaviour? What is the intuition here, since its the opposite of how many mainstream OO languages work (they introduce the static keyword for the top case, which makes you explicitly say you want a static variable)? To newcomers to Python, this is a surprise.

Also, it seems you are allowed to have an instance variable and a class variable with the same name:

class x(object):
    mem = []

    def __init__(self):
        self.mem = []

I would have to run this to figure out what would be printed. I can’t even guess!

  • 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-14T08:11:13+00:00Added an answer on June 14, 2026 at 8:11 am

    The intuition is that in Python, everything is an object, including classes themselves. There’s no such thing as a “static” keyword in Python; there are classes, which are objects, and those classes have attributes. Everything that appears in the class definition is a class attribute — that includes both methods and other kinds of attributes.

    The benefit of this design is simplicity and consistency. There’s no distinction between public or private, or between static and non-static attributes. The distinction between class and instance attributes emerges naturally through the class constructor. When __init__ is called (indirectly via ClassName(params)), it receives a new instance of the class via the self parameter, and it then modifies that instance directly. Everything happens explicitly, through constructs that are already defined — you don’t have to understand any new syntax or new keywords to see what’s happening when an instance is created.

    Then all you have to understand is Python’s model for attribute lookup. It works almost identically to PATH resolution in most shells. In bash, for example, when you execute a command (ls), the first entry in the path is searched, then the second and so on, and the first version of the command that’s found is the one that’s executed. But in this case, the path looks like this (at least in simple cases with single inheritance):

    instance; class; superclass; superclass; ... and so on
    

    This really isn’t so different from name resolution in nested scopes in c or c++. You complain:

    Even worse is that it seems you are allowed to have an instance variable and a class variable with the same name.

    But is that really any more confusing than the fact that c allows you to define int i; in a block, create an inner block, and then define another int i; that masks the original?

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

Sidebar

Related Questions

Using the Microsoft Driver Development Kit (DDK), this error plagued me as I attempted
this is my first question in here, and I would like to ask if
My Core Data model looks like this: alt text http://fwdr.org/lg4q The players relationship of
Sorry for the very involved question, but this is something I've been researching for
This problem has plagued me for quite some time. I always just work around
Enter, Apache Maven : Like many things in the software world, Maven seems to
This has plagued me on and off for years. Can anyone explain why $(HOSTNAME)
I like the immutability concept but sometimes I wonder, when an application isn't meant
Unable to determine what is going on with this error. I am uploading a
I'm sure that the big project I'm working on is plagued with this problem.

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.