I'm working on a project for Uni and I need to do some of it in Python. I think I understand the basics, but I'm getting some behaviour that seems really strange to me. I have a class where multiple objects seem to share the same data and I have no idea why.
This is the relevant code from the class with the problem (I have verified this by typing the definition straight into the interpreter as well).
And this is what I get from the interpreter when I try and use the class.
And just for good measure - the output from the wholesale interpreter version.
So, anyone have any ideas? What am I missing?
Thanks in advance.
This is the relevant code from the class with the problem (I have verified this by typing the definition straight into the interpreter as well).
Code:
class SusitFrame:
def __init__(self, type = None, length = -1, frameNum = -1, channel = None, content = []):
self.type = type
self.length = length
self.frameNum = frameNum
self.channel = channel
self.content = content
def addContent(self, newcontent):
self.content.append(newcontent)
And this is what I get from the interpreter when I try and use the class.
Code:
>>> frame1 = susit.SusitFrame()
>>> frame2 = susit.SusitFrame()
>>> frame1.addContent('hello')
>>> frame1.content
['hello']
>>> frame2.content
['hello']
And just for good measure - the output from the wholesale interpreter version.
Code:
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class SusitFrame:
... def __init__(self, type = None, length = -1, frameNum = -1, channel = None, content = []):
... self.type = type
... self.length = length
... self.frameNum = frameNum
... self.channel = channel
... self.content = content
... def addContent(self, newcontent):
... self.content.append(newcontent)
...
>>> frame1 = SusitFrame()
>>> frame1
<__main__.SusitFrame instance at 0x1d5620>
>>> frame2 = SusitFrame()
>>> frame1.content
[]
>>> frame2.content
[]
>>> frame1.addContent('hello')
>>> frame1.content
['hello']
>>> frame2.content
['hello']
>>>
So, anyone have any ideas? What am I missing?
Thanks in advance.