Python seems to treat instance variable as a class variable

Viewed 251

I have an instance variable that seems to be treated like a class variable since it changes all instances of the object.

class DNA(object):

      def __init__(self,genes = pd.DataFrame(), sizecontrol=500, name='1'):
        self.name = name
        self.genes = genes  # This attribute should be an instance variable 
        self.GeneLen = self.genes.shape[1]
        self.sizecontrol = sizecontrol
        self.Features = []
        self.BaseFeats = []
        random.seed(self.name)

When I run this I get the following:

 In[68]: df = pd.DataFrame(data)

 In[69]: x1 = DNA(genes=df)

 In[70]: x2 = DNA(genes=df)

 In[71]: x1.genes["dummy"] = 'test'

 In[72]: x2.genes["dummy"].head(4) 
 Out[72]:   
  0 test 
  1 test 
  2 test 
  3 test 

How can I make sure x1.genes does not affect x2.genes?

2 Answers
Related