Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because <code>configuration</code> is a class variable and not an instance variable. Fixing this should fix your problem.</p> <pre><code>class container(object): def __init__(self): self.name = 'container' self.configuration = {'var1': 'var1', 'var2': 'var2'} </code></pre> <p>What's going on here is that <code>configuration</code> ends up living in <code>containter.__dict__</code> instead of in the dictionaries of its instances when you make it a class variable. This means that <code>c.configuration</code> is just accessing <code>container.__dict__['configuration']</code> for all instances <code>c</code>.</p> <p>For any class variable, an assignment of the form <code>c.foo = x</code>, creates an entry for <code>foo</code> in <code>c.__dict__</code> which shadows its entry in <code>container.__dict__</code>. So a lookup will return that first. If you delete it, then a lookup will go back to retrieving the class instance. You could do</p> <pre><code>c = container() c.configuration = x </code></pre> <p>and then <code>c.configuration</code> would be whatever <code>x</code> was. But inserting a key isn't an assignment, it's a method call on an existing object accessed through an existing binding.</p> <p>You could get away with making <code>name</code> a class variable but if you want it to change from one instance to another then it should be an instance variable (unless you want a class wide default of course).</p> <p>so:</p> <ol> <li>An assignment (using <code>=</code>, <code>setattr</code> or directly inserting in <code>__dict__</code>) on an instance will shadow a class variable. The class variable is still there.</li> <li>A lookup (calling a method, accessing the value) on an instance will grab an instance attribute if it exists and a class variable otherwise.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload