python class behaves like dictionary-or-list-data-like
<p>In python3 console, input those:</p>

<pre><code>&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.version_info
sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
&gt;&gt;&gt; type(sys.version_info)  # this is class type
&lt;class 'sys.version_info'&gt;
&gt;&gt;&gt; sys.version_info[0:2]  # ?? But it acts like a list-data-like
(3, 4)
</code></pre>

<p>My questions are:</p>

<ol>
<li>How can a class act like dictionary-or-list-data-like? </li>
<li>May give an example to construct a class like this? </li>
<li>Is there some documentation about
this?</li>
</ol>

<p>i think in python like ECMAScript (aka javascript) class is a dictionary or <strong>associative array</strong>(<a href="https://en.wikipedia.org/wiki/Associative_array" rel="nofollow">associative array</a>). since you can add a property or method to your class at runtime.(<a href="http://slides.com/alirezaafzalaghaei/introduction-to-javascript#/7/3" rel="nofollow">see</a>)</p>

<pre><code>class A(object):
    def __init__(self):
        self.x = 0
a = A()
a.y=5
print a.y # 5
</code></pre>

<p>if you want write a class like that you can use <code>__getitem__</code> and <code>__setitem__</code> methods:</p>

<pre><code>class A(object):
    class B(object):
        def __init__(self, x, y):
            self.vals = (x, y)
        def __getitem__(self, key):
            return self.vals[key]
        def __setitem__(self, key, val):
            self.vals[key] = val
        def __len__(self):
            return len(self.__vals)
    def __init__(self, x, y):
        self.b = self.B(x,y)



a = A('foo','baz')

print type(a.b) # __main__.b     __main__ because we run script straightly
print a.b[:]    # ('foo', 'baz')
</code></pre>

python list class dictionary