473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Python object instance inheriting changes to parent class by another instance

I am confused by this behaviour of Python(2.6.5), can someone shed light on why this happens?

Expand|Select|Wrap|Line Numbers
  1. class A(): 
  2.     mylist=[]  
  3.  
  4.  
  5. class B(A):
  6.     j=0
  7.     def addToList(self):
  8.         self.mylist.append(1)
  9.  
  10.  
  11. b1 = B()
  12. print len(b1.mylist)  # prints 0 , as A.mylist is empty
  13. b1.addToList()
  14. print len(b1.mylist)  # prints 1 , as we have added to A.mylist via addToList()
  15.  
  16. b2 = B()
  17. print len(b2.mylist)  # prints 1 , not 0 !!! Why ?????
  18.  
Jul 17 '10 #1
2 1571
bvdet
2,851 Expert Mod 2GB
The list is initialized one time when class A is defined. When instance method addToList() is called and no attribute named muylist exists in the local scope, the interpreter finds mylist in the scope of A and appends 1 to it. If you want a new list created every time you create an instance, implement an __init__() function.

Example:
Expand|Select|Wrap|Line Numbers
  1. class A:
  2.     def __init__(self):
  3.         self.mylist = []
  4.  
  5. class B(A):
  6.     def __init__(self):
  7.         A.__init__(self)
  8.  
  9.     def addToList(self):
  10.         print self
  11.         print self.__dict__
  12.         self.mylist.append(1)
Some interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> b1 = B()
  2. >>> b1.mylist
  3. []
  4. >>> b1.addToList()
  5. <__main__.B instance at 0x00F6E030>
  6. {'mylist': []}
  7. >>> b1.mylist
  8. [1]
  9. >>> b2 = B()
  10. >>> b2.addToList()
  11. <__main__.B instance at 0x00F6E6E8>
  12. {'mylist': []}
  13. >>> b2.mylist
  14. [1]
  15. >>> 
Jul 18 '10 #2
mylist is a class variable and this is shared by all A (instance or not)
Jul 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Edward Diener | last post by:
Is there a way in Python to have the constructor of a class "return" another instance of the same class ? I am well aware of the fact that __init__ does not return anything, but I would love to do...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
3
by: Dan Holmes | last post by:
If i have Class2 that is inherited by Class1, how can Class1's methods operate on Class2's assembly information? For instance: public class Component { public virtual List<ComponentInfo>...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
1
by: =?Utf-8?B?R2FsYWhhZA==?= | last post by:
I have an ascx file that inherits an abstract class. Within this abstract class are protected properties. I can access these properties within the page_load event of the parent class, however when...
5
by: devgupta01 | last post by:
One.java ------------ public class One{ int x; char y; One(){x=0;y='a';} public void disp(){System.out.println("x ="+x+", y ="+y);} }
4
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I had a class that worked, but I knew wasn't designed correctly from an OO point of view, so I changed it so it was more like I thought it 'should' be, and now it doesn't always work....
7
by: Tarscher | last post by:
Hi all, I have classes: Unit, Transporter, Bus and Car Transporter inherits from Unit and Bus and Car inherit from Transporter. I want to be able to do Bus bus = new Bus() if (bus is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.