Python: how make class, inherited from list, with access to first element?
<p>In Python I would like to make class <em>MyList</em> and inherit from <em>list</em>. Besides I would like to make attribute <em>first</em>, which is equal to first element of list.
For example, for such code </p>

<pre><code>l = MyList([0, 1, 2])
print (l.first)
l.first = 3
print (l)
</code></pre>

<p>I would like to see</p>

<pre><code>0
[3, 1, 2]
</code></pre>

<p>I write</p>

<pre><code>class MyList(list):
    def __init__(self, input_list):
        self.first = input_list[0]

my_list = MyList([0, 1, 2])
print (my_list)
print (my_list.first)
</code></pre>

<p>and get</p>

<pre><code>[]
0
</code></pre>

<p>Why my_list is empty?</p>

<p>Define <code>first</code> as a <a href="https://docs.python.org/2.7/library/functions.html#property" rel="nofollow"><code>property</code></a> rather than a simple attribute.</p>

<pre><code>class MyList(list):
    @property
    def first(self):
        return self[0]

    @first.setter
    def first(self, val):
        self[0] = val
</code></pre>

<p><strong>Demo:</strong></p>

<pre><code>&gt;&gt;&gt; lst = MyList([0, 1, 2])
&gt;&gt;&gt; lst.first
0
&gt;&gt;&gt; lst
[0, 1, 2]
&gt;&gt;&gt; lst.first = 100
&gt;&gt;&gt; lst
[100, 1, 2]
</code></pre>

python class