Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could subclass the <a href="http://docs.python.org/dev/library/collections.html#collections.OrderedDict" rel="nofollow">collections.OrderedDict</a> class. For example:</p> <pre><code>import collections class Child(object): def __init__(self, id, name): self.id = id self.name = name def __repr__(self): return 'Child(\'%s\', \'%s\')' % (self.id, self.name) class MyOrderedDict(collections.OrderedDict): def __init__(self, *args, **kwds): super(MyOrderedDict, self).__init__() if len(args) &gt; 0: for i in args[0]: super(MyOrderedDict, self).__setitem__(i.id, i) def __getitem__(self, key): if isinstance(key, int): return super(MyOrderedDict, self).__getitem__(self.keys()[key]) if isinstance(key, slice): return [super(MyOrderedDict, self).__getitem__(k) for k in self.keys()[key]] return super(MyOrderedDict, self).__getitem__(key) def append(self, item): super(MyOrderedDict, self).__setitem__(item.id, item) def pop(self, key = None, default = object()): if key is None: return self.popitem() return super(MyOrderedDict, self).pop(self.keys()[key], default = default) class Classroom(object): def __init__(self, children): self.children = MyOrderedDict(children) classroom = Classroom([Child('389', 'pete')]) print repr(classroom.children[0]) classroom.children.append(Child('2344', 'joe')) print repr(classroom.children.pop(0)) print repr(classroom.children['2344']) print repr(classroom.children[0:1]) </code></pre> <p>This code outputs:</p> <pre><code>Child('389', 'pete') Child('389', 'pete') Child('2344', 'joe') [Child('2344', 'joe')] </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.
 

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