Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:39:09+00:00 2026-06-18T14:39:09+00:00

I have a dictionary with IDs as keys and classes as entries. When I

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:39:11+00:00Added an answer on June 18, 2026 at 2:39 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, one nested in the other. Public Class Operation Public Property
I have a dictionary with tuples as keys. e.g.: {('tags','1'): 'name', ('name','first'):'rik', ('name','last'):'atee'} In
I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have a dictionary called fsdata at module level (like a global variable). The
I have a dictionary dict2 which I want to iter through and remove all
I have created on2many field in class A and other field nombre (integer): 'Inventaire'
I have a dictionary of timestamps in this form 2011-03-01 17:52:49.728883 and ids. How
I have 2 dictionaries with the same keys: d={123:'bla', 456: blabla} e={123:'bla', 456:''} Dictionary
I have two or more arrays -- one with IDs, one or more with
I have a NSMutableArray containing NSMutableDictionary values. I have to identify a particular dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.