Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several issues in your code:</p> <p>1.If you make any list operation the result will be a native list:</p> <pre><code>layers1 = Layers() layers2 = Layers() layers1 + layers2 -&gt; the result will be a native list </code></pre> <p>2.Why define input_string when you can override <a href="http://docs.python.org/reference/datamodel.html#object.__repr__" rel="noreferrer"><code>__repr__</code></a> or <a href="http://docs.python.org/reference/datamodel.html#object.__str__" rel="noreferrer"><code>__str__</code></a></p> <p>3.Why do you even have to derive from list in this case? You only need to derive from list if you want your class to behave exactly like a list. But in your case you seem to be looking for a container. All you need to do to get your class to behave similar to a list is to override some special python methods <a href="http://docs.python.org/reference/datamodel.html#emulating-container-types" rel="noreferrer">http://docs.python.org/reference/datamodel.html#emulating-container-types</a></p> <pre><code>class Layers(object): def __init__(self, container=None): if container is None: container = [] self.container = container def add(self,n,k,comment): self.container.append([n,k,comment]) def __str__(self): return str(self.container) def __repr__(self): return str(self.container) def __getitem__(self, key): return Layers(self.container[key]) def __len__(self): return len(self.container) &gt;&gt;&gt; l = Layers() &gt;&gt;&gt; l.add(1, 2, 'test') &gt;&gt;&gt; l.add(1, 2, 'test') &gt;&gt;&gt; l [[1, 2, 'test'], [1, 2, 'test']] &gt;&gt;&gt; l[0] [1, 2, 'test'] &gt;&gt;&gt; len(l) 2 </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