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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:15:53+00:00 2026-05-14T23:15:53+00:00

Code in test.py: class Base(object): def __init__(self, l=[]): self.l = l def add(self, num):

  • 0

Code in test.py:

class Base(object):
    def __init__(self, l=[]):
        self.l = l

    def add(self, num):
        self.l.append(num)

    def remove(self, num):
        self.l.remove(num)

class Derived(Base):
    def __init__(self, l=[]):
        super(Derived, self).__init__(l)

Python shell session:

Python 2.6.5 (r265:79063, Apr  1 2010, 05:22:20) 
[GCC 4.4.3 20100316 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> a = test.Derived()
>>> b = test.Derived()
>>> a.l
[]
>>> b.l
[]
>>> a.add(1)
>>> a.l
[1]
>>> b.l
[1]
>>> c = test.Derived()
>>> c.l
[1]

I was expecting “C++-like” behavior, in which each derived object contains its own instance of the base class. Is this still the case? Why does each object appear to share the same list instance?

  • 1 1 Answer
  • 1 View
  • 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-14T23:15:54+00:00Added an answer on May 14, 2026 at 11:15 pm

    You’re making a common Python newcomer mistake.

    See my answer here:
    How should I declare default values for instance variables in Python?

    Briefly explained, Python interprets the class definitions only once. That means everything declared in the __init__() method is only created once. Or, in another words, your [] list default argument is only made once.

    Then self.l = l assigns a reference to the same instance every time you create a new class, hence the behaviour you weren’t expecting.

    The Pythonic way is this (partial code):

    def __init__(self, arg=None):
        if arg is None:
            arg = []
        self.arg = arg
    

    Also, you should consider using a better naming convention than l, which is hard to read and might be mistaken as 1 or |.

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

Sidebar

Related Questions

Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
I want to speed up the following code using cython: class A(object): cdef fun(self):
I have a class like the following: class A: def __init__(self, arg1, arg2, arg3):
I have the following code: class Test { public static void main(String[] args) {
I have the following code require 'test_helper' class ApplicationControllerTest < ActionController::TestCase test should display
I am using the below code in my test class , to load all
This code works: class Test { public: Test(string name) : _name(name) {}; bool operator()()
I have the following code: public class Test extends JFrame implements ActionListener{ private static
I have following code public class TEST { public static void main(String arg[]){ try

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.