Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main con / gotcha with using class attributes to provide default values for what you intend to be instance-specific data is that the default values are going to be shared between all instances of the class until the value is changed. E.g.:</p> <pre><code>class Foo(object): a = [] foo1 = Foo() foo2 = Foo() foo1.a.append(123) foo1.a # [123] foo2.a # [123] </code></pre> <p>However, the following will work as one might expect:</p> <pre><code>class Bar(object): a = 123 bar1 = Bar() bar2 = Bar() bar1.a = 456 bar2.a # 123 </code></pre> <p>To avoid this gotcha while using this technique, you should only use it to set defaults that are <em>immutable</em> values. (E.g. numbers, strings, tuples…)</p> <p>The reason why Python behaves this way is that when you access an attribute with:</p> <pre><code>foo.bar </code></pre> <p>then <code>bar</code> is first looked up in the object <code>foo</code>. If the name in not found in the object (i.e. in <code>foo.__dict__</code>), then the name is looked up in the type of that object. For example, this mechanism is part of how method lookups work. (If you look at the <code>__dict__</code> of an object, you'll notice its methods aren't there.)</p> <p>Other minor issues are that this exposes the defaults through the type object when they're intended to be instance-specific; and that it mixes the definitions of class-specific attributes (like constants) if you have any with the defaults. The corollary of the former is that this will let you redefine the value of the default later on for all objects that haven't changed the value yet by assigning to the class attribute. (This could be useful, or confusing; the same precaution against mutable "global" variables applies.)</p>
    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.
    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