I have an issue with object creation with a Python programme I'm trying to make. I'm new to classes so not sure if this an obvious issue or not.
I have two classes:
class transaction():
def __init__(self, amount, name, typ, bank, date):
self.amount = Decimal(amount)
self.name = name
self.type = typ
self.bank = bank
self.date = date
self.day = date[8:]
self.month = date[5:7]
self.year = date[:4]
def compare(self, other_trans):
result = True
for attrib in other_trans.__dict__.keys():
if other_trans.__getattribute__(attrib) != self.__getattribute__(attrib):
result = False
break
return result
class full_date():
transactions = []
def __init__(self, lst, date):
for trans in lst:
if trans.date == date:
self.transactions.append(trans)
self.date = date
true_list = self.transactions[:]
duplicate = True
indexer = 0
while duplicate:
duplicate = False
for trans2 in self.transactions[indexer:]:
indexer = self.transactions.index(trans2)
testlist = self.transactions[:]
del testlist[testlist.index(trans2)]
for point in testlist:
if point.compare(trans2):
duplicate = True
del true_list[true_list.index(point)]
break
else:
duplicate = False
if duplicate:
break
self.transactions = true_list
transaction
is supposed to be a manipulatable object for analysis (financial transactions) whereas full_date
is an object to store these transactions. I know in theory I could do this with a dictionary but I'm using this as practice for learning how to use classes and the __init__
function has given me a nifty way to clean out duplicate transactions.
However, for some unknown reason, when I try to create multiple instances of the full_date class, they just copy themselves so I end up with 2 objects exactly the same.
Here is what I'm trying:
>>> y = transaction("12.51", "Amazon", "Other", "HSBC", "2014-12-06")
>>> x = transaction("12.49", "Amazon", "Other", "HSBC", "2014-12-06")
>>> w = transaction("12.50", "Amazon", "Other", "HSBC", "2014-12-06")
>>> lst = [w,x,y,z]
>>> b = full_date(lst, "2014-12-06") #a populated list so b.transactions should be populated
>>> b.transactions #and is
[<__main__.transaction object at 0x7fbcc37e5bd0>, <__main__.transaction object at 0x7fbcbf3a3e10>, <__main__.transaction object at 0x7fbcc37e5c10>]
>>> a = full_date([], "2014-12-06") #an empty list so a.transactions shouldn't be populated
>>> a.transactions #but it is
[<__main__.transaction object at 0x7fbcc37e5bd0>, <__main__.transaction object at 0x7fbcbf3a3e10>, <__main__.transaction object at 0x7fbcc37e5c10>]
This also happens when I try to clear an object after I've defined it:
>>> b = None
>>> b = full_date([], "2014-12-05") #an empty list so b.transactions should be empty - also has the wrong date to make it more confusing
>>> b.transactions #but it is
[<__main__.transaction object at 0x7fbcc37e5bd0>, <__main__.transaction object at 0x7fbcbf3a3e10>, <__main__.transaction object at 0x7fbcc37e5c10>]
Does anyone know why this is happening?
Edit: Found the answer.
class full_date:
def __init__(self, args):
self.transactions = []
... code
as opposed to:
class full_date:
transactions = []
def __init__(self, args):
... code