Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define the <code>y</code> var on the class-level attribute. You will need to initialize it to something, even it's an empty list (as you were doing before).</p> <pre><code>&gt;&gt;&gt; class X: ... y = [] ... def __init__(self): ... pass </code></pre> <p><strong>Update based on your comments:</strong></p> <p>You mentioned that you were mixed on the terminology (I'm assuming between class and instance variables), so here's what'll happen if you use this class (and is probably not what you want).</p> <pre><code>&gt;&gt;&gt; class X: ... y = [] # Class level attribute ... def __init__(self): ... pass ... &gt;&gt;&gt; x = X() &gt;&gt;&gt; x.y.append(1) &gt;&gt;&gt; x.y [1] &gt;&gt;&gt; x.y.append(2) &gt;&gt;&gt; z = X() &gt;&gt;&gt; z.y.append(3) &gt;&gt;&gt; z.y [1, 2, 3] &gt;&gt;&gt; X.y.append(4) &gt;&gt;&gt; [1, 2, 3, 4] </code></pre> <p>Notice that when you add a variable to the class it sticks around between constructions. In the previous code we instantiated the variable <code>x</code> and the variable <code>z</code> as an instance of X. While we added to the <code>y</code> variable in the <code>z</code> instance, we still appended to the class variable <code>y</code>. Note on the very last line (<code>X.y.append(4)</code>) I append an item using a reference to the class <code>X</code>.</p> <p>What you probably want is based off of your original post:</p> <pre><code>&gt;&gt;&gt; class X: ... def __init__(self, y=None): ... self.y = y or list() # Instance level attribute. Default to empty list if y is not passed in. ... &gt;&gt;&gt; x = X() &gt;&gt;&gt; x.y.append(1) &gt;&gt;&gt; x.y [1] &gt;&gt;&gt; z = X() &gt;&gt;&gt; z.y.append(2) &gt;&gt;&gt; z.y [2] &gt;&gt;&gt; &gt;&gt;&gt; s = X() &gt;&gt;&gt; s.y [] &gt;&gt;&gt; t = X(y=[10]) &gt;&gt;&gt; t.y [10] </code></pre> <p>Notice how a new list is created with each instance. When we create a new instance <code>z</code> and try to append to the <code>y</code> variable we only get the value that was appended, rather than keeping all of the existing additions to the list. In the last instantiation (<code>t</code>) example the constructor passes in the <code>y</code> parameter in it's construction, and thus has the list <code>[10]</code> as it's <code>y</code> instance variable.</p> <p>Hopefully that helps. Feel free to ask any more uncertainties as comments. Also, I would suggest reading up on the Python class documentation <a href="http://docs.python.org/tutorial/classes.html" rel="nofollow noreferrer">here</a>.</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.
 

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