0

I have a dictionary with IDs as keys and classes as entries. When I try to append to the variable of one particular class, all other classes in the dictionary are appended to also.

class Reaction:
    def __init__(self, bkm_id, ec_nums = [], b_ids = [], k_ids = [], m_ids = [], source = ''):
        self.bkm_id = bkm_id
        self.ec_nums = ec_nums
        self.b_ids = b_ids
        self.k_ids = k_ids
        self.m_ids = m_ids
        self.source = source

        self.substrates = []
        self.products = []

    def add_metabolite(self, metabolite, stoichiometry, subs_prod):
        if subs_prod == 'S':
            self.substrates.append(Substrate_Product(metabolite, stoichiometry))
        elif subs_prod == 'P':
            self.products.append(Substrate_Product(metabolite, stoichiometry))   

defines the class. Then if I type:

rxna = Reaction('a')

rxnb = Reaction('b')

dict = {}

dict['a'] = rxna

dict['b'] = rxnb

dict
Out[14]: 
{'a': <__main__.Reaction instance at 0x102c51fc8>,
 'b': <__main__.Reaction instance at 0x102c62518>}

dict['a'].b_ids.append('BID')

dict['a'].b_ids
Out[16]: ['BID']

dict['b'].b_ids
Out[17]: ['BID']

rxnb.b_ids
Out[18]: ['BID']

whereas I only wanted to append 'BID' to rxna.b_ids. Am I missing something obvious?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Will Bryant
  • 521
  • 5
  • 17

1 Answers1

2

These lists are created once, on class definition, and therefore are shared by all instances. You wanted:

class Reaction:
    def __init__(self, bkm_id, ec_nums=None, b_ids=None, k_ids=None, m_ids=None, source = '')
        self.bkm_id = bkm_id
        self.ec_nums = ec_nums
        self.b_ids = b_ids or []
        self.k_ids = k_ids or []
        self.m_ids = m_ids or []
        self.source = source

        self.substrates = []
        self.products = []

    def add_metabolite(self, metabolite, stoichiometry, subs_prod):
        if subs_prod == 'S':
            self.substrates.append(Substrate_Product(metabolite, stoichiometry))
        elif subs_prod == 'P':
            self.products.append(Substrate_Product(metabolite, stoichiometry)) 

This is why you never use mutable objects as argument defaults (well, unless you have a good reason).

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124