Recently run into following problem using python 2.7: I have class like this:
class Comment():
def __init__(self, preComments = [], postComment = ''):
self.__preComments = preComments
self.__postComment = postComment
I use code like this multiple times:
# self.commentQueue holds comment.Comment()
def getQueuedComment(self):
queuedComment = self.commentQueue
self.commentQueue = comment.Comment()
return queuedComment
Idea of this code is to return instance of Comment and create new instance in place, so queuing may continue.
And result is that weirdly, but each time second code is called self.commentQueue
holds data from all other instances of this class(data are appended not assigned and problem occurs only with list), but as I understand that self.commentQueue = comment.Comment()
should create new, empty class, by empty I mean that self.__preComments = []
and self.__postComment = ''
.
Fix is to call self.commentQueue = comment.Comment([], '')
instead of self.commentQueue = comment.Comment()
, but I just don't understand why this happens.