Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Variables <code>a</code>, <code>b</code>, and <code>c</code> are class variables. They're evaluated and set once, when the class is first created. Variables <code>x</code>, <code>y</code>, and <code>z</code> are instance variables, which are evaluated and set whenever an object of that class is instantiated.</p> <p>In general, you use class variables if the same values are going to be used by every instance of the class, and only needs to be calculated once. You use instance variables for variables that are going to be different for each instance of that class.</p> <p>You can access class variables through the instance variable syntax <code>self.a</code>, but the object in question is being shared between all instances of the class. This doesn't affect much when using immutable data types such as integers or strings, but when using mutable data types such as lists, appending to <code>self.a</code> would cause all instances to see the newly-appended value.</p> <p>Some examples from IDLE are probably helpful in understanding this:</p> <pre><code>&gt;&gt;&gt; class Foo(object): a = 'Hello' b = [] def __init__(self): self.y = [] &gt;&gt;&gt; instance_1 = Foo() &gt;&gt;&gt; instance_2 = Foo() &gt;&gt;&gt; instance_1.a 'Hello' &gt;&gt;&gt; instance_2.a 'Hello' &gt;&gt;&gt; instance_1.a = 'Goodbye' &gt;&gt;&gt; instance_1.a 'Goodbye' &gt;&gt;&gt; instance_2.a 'Hello' &gt;&gt;&gt; instance_1.b [] &gt;&gt;&gt; instance_2.b.append('12345') &gt;&gt;&gt; instance_1.b ['12345'] &gt;&gt;&gt; instance_2.y [] &gt;&gt;&gt; instance_2.y.append('abcde') &gt;&gt;&gt; instance_2.y ['abcde'] &gt;&gt;&gt; instance_1.y [] </code></pre>
    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. 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